Showing posts with label Axis 2. Show all posts
Showing posts with label Axis 2. Show all posts

Thursday, November 20, 2014

Create Web service client with using Axis2 in Eclipse

Create web service client with using Axis2 in eclipse

Web service helps to communicate the different application with out consuming more time even the applications are deployed in different platform. The reason behind was, the all communication is in XML. In previous tutorials briefly explain that how to create the WSDL and How to write the web service and deploy to the server and test. 

In this tutorial you able to learn that, to build the client with using the WSDL generated here

Objective : Create a web service client with using the Axis2 in eclipse.

Scope : Create we web service client with using the Axis2 and test the client service.

Requirements : 
  • Ecipse Java EE IDE
  • Axis2 - 1.6.2
  • Apache tomcat 6.0.26
Step 1 : Create a Dynamic web project  with using eclipse and name it as "ServiceClient". Right click on the "ServiceClient" project and create a web service client with using eclipse. 

The way of creating the web service client in eclipse mentioned as below. 

       New > Other > Web Service > Web Service Client


Step 2 : Chose the WSDL, which we created in the previous article. In additionally in the client generation wizard, select the Axis2 as web service run time and Tomcat 6 as a server. Click the next button to continue the wizard process.




Step 3 : Click the next button with out changing any of the parameters and finally click the finish button. Once you pressed the finish button, the auto generated code will added to the  "ServiceClient" project. (The auto generated classes are HelloworldCallbackHandler .java and HelloworldStub.java


Step 4 : In order to test the generated client, you have to write a separate class in java. In our example in order to test the client, you able to use the following "TestClient" class.

/ServiceClient/src/findanidea/webservice/services/helloworld/TestClient.java


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

import java.rmi.RemoteException;

import findanidea.webservice.services.helloworld.HelloworldStub;
import findanidea.webservice.services.helloworld.HelloworldStub.SayHello;


public class TestClient {
 public static void main(String[] args) throws RemoteException {
  HelloworldStub stub = new HelloworldStub();
  SayHello sayHello = new SayHello();
  sayHello.setName("John");
  System.out.println(stub.sayHello(sayHello).getOutput());
 }
}


Once you execute the above mentioned "TestClient" class, you able to retrieve the "Hello... John" message in application console.



Hope its helps.... 

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