Axis2 Web Service and Java Client…

I googled hours for java client for axis 2 web service with the help of  WSO2 Application Server .Finally figured out by looking at a written code by some one else. This post just covers very basic things. In this article I m gonna show you folks how to write simple web service using axis2. , and a simple java client to consume the web service.  Thing we are gonna do in this tutorial mentioned below as steps.

1. Write a axis 2 web service called “CalculaterService” which contained one operation “add”,which takes two numbers and returns the addition of those two.

2.Write a simple java client to consume the web service,that means,  we provide two numbers to the web service and get the addition of it.

So enough talking, Let’s get started….

you will need following servers and Tools to do this in that quick… Other wise it will take some time :).

  1.  WSO2 Developer Studio –  One of the best IDEs I’ve ever seen , Creating projects like Axis2 is just one click away . You can download it here.
  2. WSO2 Application Server – We use this to host our Axis2 web service. We don’t use this just for hosting the web service. As you  go through the tutorial, you will see a bit of it’s capabilities. 🙂 You can download it from here.

If you have downloaded these two.. now its time get our hands dirty.

Fisrt, Let’s create the web service. In this post my intention is not to describe you fundamentals of web services, so I assume that you have at least a tiny knowledge about web services.

run the eclipse …

In the menu bar go to Developer Studio -> Open Dashboard.

You will see a screen like below.

t

Click on Axis2 Service Project to create the web service.

In next window you see , select Create New Axis2 Service. and click Next.

Then enter following details on relevant fields.

Project Name : Calculator

Package: com.example.calculator

Class : CalculatorService

Then click next, and then click  Finish.

In this service class, let’s implement addition as an operation . We can add operations to the service as public methods. So they are consumable as service. In the mean time , we can define private methods to be used in side the service. Those won’t be exposed as operations in the web service.

package com.example.cal;
public class CalculatorService{</pre>
 public int Addition(int firstNum,int secondNum) {
 return firstNum+secondNum;
 }

}

Then Right Click on the Calculator project in the project explorer.

and click on  Export Project as Deployable Archive .There you will see a window like below. Give the destination for the resulting file and click finish.

Selection_124

So in the destination you will find a file with name Calculator_1.0.0.aar

hmm.. we are done with creating simple web service . now we are going to deploy it on wso2 application server and test with the Try it feature of wso2 applicaiton server.:)

So now let’s start application server. If you are a linux user goto downloaded and extracted folder and and run following command


sh bin/wso2server.sh

if you are a windows user run wso2server.bat file which can be found in <AS_HOME>/bin/

(AS_HOME is extracted folder of wso2 as)

go to the link

https://localhost:9443/carbon/

then login with following credentials

user name: admin

password: admin

Then you will see a screen like below.

Selection_126

You will see a menu on left side..

Under the sub section Add , click on AAR Service.

Selection_125

Then click on Choose File and select that .aar file exported form wso2 developer studio.

And then click Upload.

Then click on List under the Service tab.

Selection_127

There you will see list of web services deployed in the application server. You will see “Calculator Service” among them if nothing has went wrong ;).

Selection_129

Click on it and then you will see a screen. Let’s test the web service using Try it.

Selection_130

in the left side window set payload as


<body>
 <p:Addition xmlns:p="http://cal.example.com">
 <!--0 to 1 occurrence-->
 <xs:firstNum xmlns:xs="http://cal.example.com">3</xs:firstNum>
 <!--0 to 1 occurrence-->
 <xs:secondNum xmlns:xs="http://cal.example.com">2</xs:secondNum>
 </p:Addition>
</body>

and click on  Send. Then you will see a result like below in the right side window.


<ns:AdditionResponse xmlns:ns="http://cal.example.com">
 <ns:return>5</ns:return>
</ns:AdditionResponse>

.

Our web service is working correctly .. Now let’s begin the second part .. writing a simple java client to consume the web service.

Now let’s start writing the client. Create New Java Project 

Add a class “MyClient”  to the project with package name com.my.client .

Now we need to get those generated classes in this project.

Again go to Developer Studio dash board…

click on Generate Axis2 Web Service Client

Selection_135

Then we need to provide wsdl URI to generate clients. We can find it form the wso2 application server. In the service page click on “WSDL1.1” and copy url in the browser . And paste it in the “WSDL URI” field as below.

WDL URI is like “http://localhost:9763/services/CalculatorService?wsdl&#8221;

Selection_136

Then click next and set the source folder of the MyClient project. Then add main method to the MyClient class as below.


public static void main(String[] args) {
 try {
 //create service stub with the endpoint of the sercer
 CalculatorServiceStub stub=new CalculatorServiceStub("http://localhost:9763/services/CalculatorService/");
 //calling for addition operation in the service
 Addition add=new Addition();
 //passing input parameters to operation
 //setting them with the
 add.setFirstNum(3);
 add.setSecondNum(4);
 //consume the service using stub and get the response
 AdditionResponse response=stub.addition(add);

 System.out.println("Reponse from Axis2 Service: "+response.get_return());
 } catch (AxisFault e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (RemoteException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 }

Before running the sample .. add following jars to the class path.

  • axis-wsdl4j-1.5.1.jar
  •  commons-codec-1.4.jar
  • commons-httpclient-3.1.jar
  • commons-logging-1.0.4.jar
  • httpcore-4.0.1.jar
  •  neethi-2.0.4.jar
  • org.wso2.securevault-1.0.0.jar
  • XmlSchema-1.4.jar

Then run it .. 🙂 you will see the repsonse …

Advertisements