Axis2 client - Axis2 Client example

In this section we will develop client code example to access the Hello World Web service developed in the last section.

Axis2 client - Axis2 Client example

Axis2 client - Axis2 Client example

   

Apache Axis2 Client code
In this section we will develop client code example to access the Hello World Web service developed in the last section. In the last section we developed and deployed the Hello World Web service. In this section we will write the Web service client code and call the web service.

 Setting up the environment

In our first section of downloading and installing Axis 2 engine, we instructed you to download the binary version of Apache Axis2. Now extract the binary version(axis2-1.5.1-bin.zip) using any zip tool.   After extracting the file into E:\Axis2Tutorial (assuming your tutorial directory is E:\Axis2Tutorial), you will get a directory "E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1" which contains the binary version of the Apache Axis2 engine. Now set the following environment variables:

 a) AXIS2_HOME=E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1
 b) Add E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1\bin into path
 c) Add E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1\lib\* into CLASS_PATH

After making the above changes the wsdl2java.bat is available for generating the client code calling Web service.

Generating the client code using wsdl2java.bat tool

Now create a new directory E:\Axis2Tutorial\Examples\HelloWorld\client and then open dos prompt and go to same directory. Now generate the client code using following command:

WSDL2Java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -o client

Here is the output of above command.


E:\>cd E:\Axis2Tutorial\Examples\HelloWorld\client

E:\Axis2Tutorial\Examples\HelloWorld\client>WSDL2Java.bat -uri http://localhost:
8080/axis2/services/HelloWorldService?wsdl -o client
Using AXIS2_HOME: E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1
Using JAVA_HOME: E:\JDK\jdk1.6.0_03
Retrieving document at 'http://localhost:8080/axis2/services/HelloWorldService?wsdl'.

The above command will generate a) HelloWorldServiceStub.java and b) HelloWorldServiceCallbackHandler.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory.

Now run cd to client/src directory.

cd client/src

Developing the code to call the service

Create the file Test.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory. The code of Test.java file is as below:

package net.roseindia; 
import net.roseindia.*; 
import net.roseindia.HelloWorldServiceStub.SayHello;
public class Test {
 public static void main(String[] argsthrows Exception {
 HelloWorldServiceStub stub = new HelloWorldServiceStub();
 //Create the request

  net.roseindia.HelloWorldServiceStub.SayHello  request = new 
   net.roseindia.HelloWorldServiceStub.SayHello
();

  request.setArgs0("Deepak Kumar");
 //Invoke the service
  net.roseindia.HelloWorldServiceStub.SayHelloResponse response 
  = stub.sayHello
(request);
  System.out.println("Response : " + response.get_return());
  }
}  

Compiling and testing the Web service

Now go to E:\Axis2Tutorial\Examples\HelloWorld\client\client\src directory and with the help of javac command compile the code.

E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.java
Note: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

To run the client type following command:
java net/roseindia/Test

Here is the output:

E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.java
Note: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>java net/roseindia/Test
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
Response : Hello : Deepak Kumar

E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>

The client appliction makes a call to Web service and in response Web services returns the Hello : Deepak Kumar message

You have successfully tested your Web Service.

Download code