Monday, November 3, 2014

Create web service with using axis2 in eclipse


Create web service with using Axis2 in eclipse

These days web service technology is widely using in the Java EE applications. The most common reason is that, the web service is the way of communicate different applications even the applications are deployed in different plat form. For example if the currency exchange web service is build in Dot Net and deployed in one server. If another application written in Java and running in different server, that able to use the currency exchange service, (The service already developed in Dot Net) via web service client. The communication can be possible with using the XML messages over the HTTP protocol.

There are main two type of web services available. One was SOAP (Simple Object Access Protocol). The second was RestFul (Representational State Transfer). 

There are two approaches commonly used, when develop the web services.
1. top to bottom approach (WSDL to Java)
2. bottom to top approach (Java to WSDL)

The below tutorial will explain for, how to create the Soap web services based on top to bottom approach with using the Axis2.

Scope : Create a web service with using Axis2 in eclipse with using top to bottom approach.

Objective : Create an web service using Axis2 and the service operation return the Welcome message with given username.

Requirements : 
  • Eclipse Java EE IDE
  • axis2-1.6.2
  • Apache Tomcat - 6.0.26
Step 1 : Create a Dynamic web project "webservice" in eclipse. The way of creating the dynamic web project mentioned as below.
                 New > Dynamic web project

Step 2 : Copy the WSDL (you able to find here ) in to the project created in the Step 1. (/webservice/helloworld.wsdl)

Step 3 : Create the web service project as mentioned in below.
Right Click on the "webservice" project > New > Other > Web Service > Web Service

Step 4 : The following screen will appear. 



In the screen choose the "Top down Java bean Web Service" as Web service Type and choose the WSDL path as Service definition.

Select the server runtime as Tomcat 6.0 and Web service runtime as Apache Axis 2.

Step 5 : Click the next button and specify the package name for the service classes which going to generate by Axis based on the WSDL.

In this example package name specified as "findanidea.webservice.services.helloworld" .

Step 6 : Click the next button, and wait... After some time the auto generated code added to your project. 

The following java classes are generated based on the WSDL provided.




Step 7 : Do not start the server at this point and finished the wizard at this level. Now the time comes to develop the service implementation.

Step 8 : In the generated "HelloworldSkeleton" class need to incorporate the business logic.

In the below code line 3 to 5 we commented, which was auto generated when create the service. In addition to that, the business logic implemented in line number 7 and 8.

For this example, the response object contains the combination of strings Hello and name as Output property. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public localhost.webservice.services.helloworld.SayHelloResponse sayHello(
   localhost.webservice.services.helloworld.SayHello sayHello) {
  /*// TODO : fill this with the necessary business logic
  throw new java.lang.UnsupportedOperationException("Please implement "
    + this.getClass().getName() + "#sayHello");*/
  
  SayHelloResponse response = new SayHelloResponse();
  response.setOutput("Hello... " + sayHello.getName());
  return response;
 }

Deploy the service to server.

Step 9 : Now the time to test the service service level, which we developed at Step 8.

Write the following "TestService" class under the below package. "findanidea.webservice.services.helloworld"

/webservice/src/findanidea/webservice/services/helloworld/TestService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package findanidea.webservice.services.helloworld;

import findanidea.webservice.services.helloworld.SayHello;

public class TestService {
 public static void main(String[] args) {
  HelloworldSkeleton skeleton = new HelloworldSkeleton();
  SayHello sayHello = new SayHello();
  sayHello.setName("John");
  System.out.println(skeleton.sayHello(sayHello).getOutput());
 }
}

Step 10 : That's all folks... Now if run the "TestService" class you able to get the following response in the console.


1
Hello... John

No comments:

Post a Comment