Wednesday, July 14, 2010

Java Web Service: Developing SOAP Based Web Service via Apache Axis2

Introduction:
Web service is a distribution software/application component that communicates using open protocols is self contained, self describing and can be discovered by UDDI (Universal Description, Discovery and Integration). Web Services can be used by other applications, or you can say other applications consume the web services. XML is the basis of the web services. XML is a language used to communicate between the different languages. Web Services are language independent and platform independent because all the vendors are agreed to use the common protocols and common way of communication using XML.
There are two main models for web services development in Java, REST based web services and SOAP/WSDL based web services.

REST based Web Services:- REST (REpresentational State Transfer) are resources in which every process is modeled as a unique URI. The protocol used in REST based web services is HTTP. These are not as secure, as flexible, as SOAP based web services. And only supports HTTP protocol.
SOAP/WSDL based Web Services:- SOAP/WSDL-based web services uses Java utilities create a WSDL file based on the Java code in the web service. The WSDL is exposed on the net. Parties interested in using the web service create a Java client based on the WSDL. Messages are exchanged in SOAP format. The range of operations that can be passed in SOAP is much broader than what is available in REST, especially in security. SOAP-based web services are suitable for heavyweight applications using complicated operations and for applications requiring sophisticated security, reliability or other standards-supported features. They are also suitable when a transport protocol other than HTTP has to be used.

Apache Axis2 is an open-source implementation of the SOAP (Simple Object-Access Protocol) submission to the W3C. Axis2 not only supports SOAP 1.1 and SOAP 1.2, but it also has integrated support for RESTful web services.

We are going to develop a simple web service using Apache Axis2 using eclipse and Tomcat 6 as server.

Setting up the Environment:
Creating a Bottom up Java Bean Web Service:

Step 1:
First step is to configure the Axis2 with tomcat. To accomplish this unzip/extract the downloaded Axis version at any location of your hard drive. I am using this path for this example: C:\axis2\axis2-1.5

Then Open Eclipse and open:
Window -> Preferences -> Web Services -> Axis2 Preferences


Select the Axis2 Runtime tab and point to the correct Axis2 runtime location and click Apply and then Ok.

Step 2:
Now we need to create a project with the support of Axis2 features. Open File -> New -> Other... -> Web -> Dynamic Web Project



Click Next and provide project name as CalculatorWebService (you can use any name). Please select the following configurations:
“Apache Tomcat v6.0” as Select Target runtime.
“2.5” as Dynamic web module version
For Configuration, Press the Modify button.


Now tick the check boxes named as “Axis2 Web Services” and select OK.

After this you will be back on the New Dynamic Web Project. Click Finish. A new project will be created.

Step 3:
Now add a new package (myservices) and a plain Java class (Calculator). This Calculator class will contain methods that we will expose as web service. The methods we want to expose as web service must have public access.





Step 4:
Now Select Calculator.java, open File -> New -> Other... -> Web Services -> Web Service




Click Next and you will be on the following screen. And make sure you Server rumtine is Tomcat v6.0 Server and Web Service Runtime should be Apache Axis2, if not you can configure these by clicking on the hyperlinks. For example to configure Web Service Runtime, click on the hyperlink highlighted as following.


And it will open a new screen to configure Apache Axis 2 runtime for you. Select Apache Axis2 from the list and click ok.


You will be back on Web Service Screen, click Next.




Again click Next.




Now Click Start Server button.


Now Click Finish. Your web service is created for your Calculator class and it is published to your Tomcat Server.
Step 5:
Right Click on the web service project and run as server.



Choose the server and click Finish.

You will see the following page in your integrated browser of eclipse.

Click on the services link encircled above and you can see your calculator service as following.

If you click on the Calculator link encircled above, you can see the WSDL generated by your web service. This WSDL you will use to create a wb service client. Please copy the link of WSDL:



The next step is to create a Web Service Client to test this web service.

Create a Web Service Client:
To create a web service client and test the web service, we will create a new project and will test this web service. Open File -> New -> Other... -> Web Services -> Web ServiceClient



Click Next.

Paste the WSDL path in the service definition box and make sure you have the correct configurations as displayed in above snapshot. And click Next.


Now leave the default configurations on this screen and click Finish. I will generate a new project that will contain the Stub class that we can use to call the methods of web service.
Add a new class in the client project to test the web service methods like shown in the following snapshot.



package myservices;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;


public class CalculatorTestClient {

/**
* @param args
* @throws RemoteException
*/
public static void main(String[] args) throws RemoteException {
//Step 1: create an object of Stub class that is generated.
CalculatorStub stub = new CalculatorStub();
//Step 2: Web Service Client has created inner classes in CalculatorStub for each of the web service method.
// We need to create the object of these classes to utilize the web service method.
CalculatorStub.Add add = new CalculatorStub.Add();
// Step 3: set the values for parameters for the web service method to call
add.setA(45.67);
add.setB(12.34);
// Step 4: Now call the web service method using the stub object and assign it to specific response variable.
CalculatorStub.AddResponse resp = stub.add(add);
// Step 5: Finally call the get_return method to retrieve the final result from response object.
System.out.println(" Result for 45.78 + 12.34 = "+resp.get_return());
}// end main

} // end class
Now finally, run this client as plain java program. Right Click inside the Java class-->Run as-->Java Application. You can see the results on the console.






NOTE:
You can also use Netbeans to create SOAP based Apache Axis2 Web Services. For this please visit the following tutorial.

3 comments:

PoL said...
This comment has been removed by the author.
PoL said...
This comment has been removed by the author.
PoL said...

Nice information and great. Thanks for sharing with us. Great job. If you want to see a collection of xcritical software visit our site