Sunday, November 27, 2011

Axis2 : Starting with a WSDL

If you are ever given a WSDL and asked create your own client and service you can follow the following steps.

Sometime back I wrote an Axis2 hello world tutorial which started with a simple Java class and used a generated client to invoke the service operation. But here we start with a given wsdl file.

You will need the following :

  • Axis2 binary distribution

  • Apache Ant (installed)

  • JDK (installed)


Generate service
Use WSDL2Java to generate service code as follows :

$ /path/to/axis2-binary-distro/bin/wsdl2java.sh -ss -sd -uw -uri service.wsdl -o service/code/dir/path

Complete Service
This is where you will add the business logic to the generated service.
Locate the file Skeleton.java file in the service/code/dir/path/src/... directory and complete the methods that you find. They are generated to throw UnsupportedOperationException.
Host Service
Do
$ ant
in the service/code/dir/path/ directory. This will generate an .aar file in service/code/dir/path/build/lib/ directory. Drop this file into /path/to/axis2-binary-distro/repository/services directory. And start axis2 server with :

$ /path/to/axis2-binary-distro/bin/axis2server.sh

Make sure service is working by pointing the browser to :
http://localhost:8080/axis2/services/service?wsdl
Generate Client
Now you can use the WSDL2Java tool once again to generate a client for the service.

/path/to/axis2-binary-distro/bin/wsdl2java.sh -uw -uri http://localhost:8080/axis2/services/service?wsdl -o client/dir/

Test
The code generated in the previous step provides a Stub.java file which provides the required methods to invoke operations of the hosted service. Now you can simply create an instance of this stub and invoke the service.