Home Webservices Axis2 Axis2 client - Axis2 Client example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Axis2 client - Axis2 Client example

Advertisement
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

   

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  

   

 
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2009

Related Tutorials

Ask Questions?    Discuss: Axis2 client - Axis2 Client example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mayur
February 23, 2013
regarding webservice

1.how i call webservice on button click event. take parameteres in gui,then call webservice on button click event. 2. This is neccessary to create such a big hierarchy for creating web services. ex:C:\Users\mayur\Desktop\TemperatureConverter\client\client\src\net\roseindia
balaji
May 15, 2012
Thanks

I tried last 10 days in many sites.but notwork.i saw your code...after 15 min write code.its working..thank you
Kumar
July 12, 2012
This is not correct.

Getting somany errors while compiling client code. There is some wrong with the procedure.
Areeg Samir
December 18, 2012
Log4j warn

I follow the steps to create client application but till the step of creating client code this error appears: C:\Users\Nicole-VM\Desktop\AXIS2\Examples\HelloWorld\client>WSDL2Java.bat -uri h ttp://localhost:8080/axis2/services/HelloWorldService?wsdl -o client Using AXIS2_HOME: C:\Users\Nicole-VM\Desktop\AXIS2\axis2-1.6.2 Using JAVA_HOME: C:\Program Files\Java\jdk1.7.0_07 Retrieving document at 'http://localhost:8080/axis2/services/HelloWorldService?wsdl'. log4j:WARN No appenders could be found for logger (org.apache.axis2.description. WSDL11ToAllAxisServicesBuilder). log4j:WARN Please initialize the log4j system properly. the log4j is existed under conf folder under Axis2. I don't know why this error appears. Any help would be appreciated?
Rajan
November 24, 2012
Re: Appreciate

Excellent example for beginners. Very easy to digest steps. Thank you Rose India.
DMCA.com