Monday, January 3, 2011

Writing a JAX-WS client to an Axis2 web service

1. Deploying the Axis2 Service

Download and launch Apache Axis2 - it comes with a sample web service which is by default available at http://localhost:8080/axis2/services/Version - we will be using this as the service to write our JAX-WS client.


The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAX-WS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints.The Reference Implementation of JAX-WS is developed as an open source project and is part of project GlassFish.

3. Axis2 and JAX-WS

4. JAX-WS client

First we need to generate the Stub classes for the service we are going to invoke. This can be done using the wsimport tool. The tool is available at [JAVA_HOME]\bin distribution.
$ pwd
$ wsimport -p org.wso2 http://localhost:8080/axis2/services/Version?wsdl
The above resulted with the following error.
[ERROR] A class/interface with the same name "wso2.Exception" is already in use. Use a class customization to resolve this conflict.
line 11 of http://localhost:8080/axis2/services/Version?wsdl
So I got to hand edit the wsdl and save it as wsdl.xml - and run wsimport against wsdl.xml
$ pwd
$ wsimport -p org.wso2 wsdl.xml
$ jar -cvf version-stub.jar *
Following is the client code - and you need to have version-stub.jar, which was generated in the previous step.


import org.wso2.Version;
import org.wso2.VersionPortType;

public class VersionClient {
 @WebServiceRef(wsdlLocation = "http://localhost:8080/axis2/services/Version?wsdl")

 public static void main(String[] args) {
  try {
   Version service = new Version();
   VersionPortType port = service.getVersionHttpSoap11Endpoint();
   String response = port.getVersion().getReturn().getValue();
  } catch (Exception e) {