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/ -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 file in the service/code/dir/path/src/... directory and complete the methods that you find. They are generated to throw UnsupportedOperationException.
Host Service
$ 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/

Make sure service is working by pointing the browser to :
Generate Client
Now you can use the WSDL2Java tool once again to generate a client for the service.

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

The code generated in the previous step provides a 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.