In my previous post, I have illustrated how to write a simple web service with Apache Axis2 and how to deploy it. But how to invoke it?
In this post, I'm going to describe how to invoke a web service using a web Service Client. For that we have to look at the WSDL file of the service we are going to invoke.
What is WSDL?
WSDL stands for Web Service Description Language. It is written in XML and it is to describe the web service and it specifies the location where the web service is located, available operations, parameters etc.
To get the WSDL file, start the axis2server and go to the link
http://localhost:8080/axis2/services/ . You may see the deployed services. Then just click on the service you are going to invoke. Then you'll be directed to the WSDL file of that service. We need to access the WSDL file when we are writing the Client code.
There are so many ways to invoke a web service. In this tutorial, I'll describe 2 ways of them.
- Using WSDL2Java tool provided with axis2
- Using Eclipse IDE with tomcat server.
In each method, by accessing the WSDL file, client stub is generated ( Client stub acts as the interface to web service and it can be considered as the client code). The communication between web service and the client is done by sending
SOAP messages.
Let's follow the first method.
1) Using WSDL2Java tool provided with axis2
In this procedure, we are going to use WSDL2Java tool to generate the client stubs, which was provided with axis2. For that go to axus2/bin/ folder. Then you will see
wsdl2java.sh and
wsdl2java.bat files.
If you are using Linux, you have to use .sh file, and if you are using windows, you have to use .bat file.
In Linux, give the following command to generate client stub.
"sh wsdl2java.sh -uri
<WSDL url> -o
/path to the client code to be generated/ "
In this case, I'm going to invoke 'CalcSumService', service and I have to give following command. Here, I'm going to generate my client code in the location of /home/Development/client/temp.
"
sh wsdl2java.sh -uri http://localhost:8080/axis2/services/CalcSumService?wsdl -o /home/Development/client/temp"
If you are using Windows, give following command.
"wsdl2java.bat -uri
<WSDL url> -o
/path to the client code to be generated/ "
In this case,
"
wsdl2java.bat-uri http://localhost:8080/axis2/services/CalcSumService?wsdl -o /home/Development/client/temp"
Now go to the folder where you were specified in the above command to generate client code. In my case /home/Development/client. You will see a build.xml file and src folder which contains the package org.apche.ws.axis2. This package CalcSumServiceCallbackHandler.java and CalcSumServiceStub.java class. Now we can see that client stub has generated. Next step is to write the client code.
This is the method we are going to invoke in the web service.
public int calcSum(int value1, int value 2){
return value1+value2;
}
Let's write a class named 'CalcSumClient' to invoke the above method in web service.
package org.apache.ws.axis2;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.apache.ws.axis2.CalcSumServiceStub.CalcSumResponse;
public class CalcSumClient {
public static void main(String[] args) throws RemoteException {
CalcSumServiceStub stub = null;
try {
stub = new CalcSumServiceStub();
} catch (AxisFault e) {
e.printStackTrace();
}
CalcSumServiceStub.CalcSum request= new CalcSumServiceStub.CalcSum();
request.setValue1(1);
request.setValue2(2);
CalcSumResponse response= stub.calcSum(request);
System.out.println(response.get_return());
}
}
The above code would invoke the web service. First we have to create a CalcSumServiceStub object. CalcSumServiceStub class contains an inner class called 'CalcSum'. Basically, for each operation in the web service, an inner class from that name of the operation in contained in the stub class. In this case, the operation in the web service we are going to invoke ic 'calcSum'. Thus we have to create an object called request from CalcSum inner class.
CalcSumServiceStub.CalcSum request= new CalcSumServiceStub.CalcSum();
Next we have to set input values for the invoking operation. In this case we have to set value1 and value2 of the 'request' object.
request.setValue1(1);
request.setValue2(2);
Then we have to pass the request object to stub.calcSum method and that method will return CalcSumResponse object.
The returning object is contained with the result coming from the web service.
Now our client code is ready. Then put the above java class into the same package where the stub and the callbackhandler is located. Then go inside the src folder, where the org.apache.ws.axis2 package is located. Then we can compile the code giveng following command.
javac -extdirs /path to axis2 /lib/ <package name> *.java
In this case give,
"
javac -extdirs /home/axis2/lib org/apache/ws/axis2/*.java"
Then the java classes are compiled. Next step is to run the client code. For that enter,
"java -Djava.ext.dirs=/Path to axis2/lib/ <packagename.Client Class name>"
In this case you have to give,
"
java -Djava.ext.dirs=/home/axis/lib org.apache.ws.axis2.CalcSumClient"
If it is correctly compiled and run, the calculated value will be displayed in the console as '3'.