Sunday, December 8, 2013

Web service


Web service 





We will use apache CXF in this session .We will create a simple web service and will use the same in by creating a web service client. I chose Apache CXF as it provides many features .Lots of documentation available on internet for Apache CXF. 

In web service we create a method called as web method and this method is exposed for remote access by specifying about it definition, how to access it remotely using a wsdl. So a web service uses the wsdl and calls the web method.


Configuring eclipse environment for creating a web service using Apache CXF 

We will use version 2.7.0 for our configuration. You can choose any version of your choice.

Firstly download Apache Cxf from  here. This is their official site.
Please download the binary version of the apache cxf.
Extract the zip file to some location on your drive.


Now open your eclipse. 
Go to windows, and then go to preference


Now click on Web Services and then click on CXF 2.x Preferences.
Now in the right side of the window click on Add button.

Browser you Apache CXF home location 
For example 
Home location = D:\apache-cxf-2.7.0\
And apche-cxf-2.7.0 folder will contain following directories
\bin
\docs
\modules
\lib 
\etc 
\samples 
\licenses

I have not mentioned them all. But the Home location will be as given above.
After finding the home folder, click on finish
Check the 2.7.0 Version in the CXF 2.x Preferences and click on apply and then click ok
Go to windows again, and then go to preference
Now click on Web Services and then click on Server and Runtime
In the right pane, select server runtime as Tomcat v6.0 server and web service runtime as apache CXF 2.x
Click on apply and then on ok


Create a server by going to File, then to new, then to other and then to server. Select apache tomcat 6.0 as server



Now we have configured apache cxf environment for creating a web service. Hope you were comfortable on settting
up the environment. 
You can find videos on youtube for settting up environment for apache CXF in eclipse.

Now we will create web service.

Create a dynamic web project in eclipse.
I created a dynamic web project with following setup done
a) Target runtime as apache tomcat
b) Dynamic web module version as 2.5
c) Configuration as apache CXF web service. (This option visible only if you have configured cxf in windows preference settings described above.
Click on Next now

Click on Next now
Click on Next now
Select CXF Runtime (We have configured earlier, hence appearing in this drop down)
Now click on Finish

Now we have created a Dynamic Web Project as FirstWebService and Server as shown below:

Now it’s time create a web service using FirstWebService Project
Create a java class as WebService.java under package com.service in src  folder  and click on Finish
Now create a method inside the class as shown
package com.service;

public class Webservice {
  
 public String sayHello(String firstName) {
       
          return "hello Mr." + firstName;
   
    }
}
Now Right click the Webservice class in Project explorer , then web services and then create web services

You will get the following window. Initially following setting are present .We will modify them as required

Move the sliders to top position for Test Service and test Client .Now click on web service runtime: Apache Axis marked with square red box
Now following windows appears. Select Apache CXF 2.x as we are using apache cxf in our web service and hit OK

Following setting we require after following above steps. Also check the Monitor the web service option and hit Next
Check the User a Service Endpoint Interface

Check the Create a SEI and enter WebServiceSEI  (you can enter a name of your choice)
Check the sayHello(String) method and Hit Next
Just Hit Next in below screen
I have unchecked the Generate separate XSD for the types in below screen and Hit Next
Hit start server in below screen 
Hit Next in below screen after starting the server.
Hit Next in below screen
Hit Next in below screen
Hit Next in below Screen
Check the second option as shown and Hit Finish in below screen
Following Screen appears in eclipse.
Let us test the web service before creating client with already available interface. Later on we will create a client.
Click on sayHello
Click on Add and enter hari as argument to our function sayHello  and Hit Go


As seen in below screen we got response as hello Mr. hari
Now if you open the My web service project , you can see all the generate class , wsdl file , beans.xml, and  also in web.xml some code auto generated by eclipse.
Open the wsdl  file  and copy the link as show
And put it browser. You will get the wsdl of the web service we have created. Notice that I have appended the highlighted link with  ?wsdl  to see the wsdl file in browser. Wsdl file pronounced as wisdal file for sake.