How-to: Write web services using Axis2

This is a very basic post on writing a web service provider and consumer using Axis2. You can find such posts anywhere in the web. Here I’ve tried to make it as simple as I can.

Set up: What all we need to do?

1. Axis2 runtime.
I tried with WAR distribution from
2. A web/app server. I am using Tomcat.

Using the Axis2 runtime & setting it up

Extract the Axis2 WAR distribution and keep it in the deploy directory of the server. In case of Tomcat keep it in webapps. This is how my directory structure look like.


The WEB-INF is the most important directory. Tell you why. Unlike our normal WEB-INF in archives, this directory hold some special sub-directories. Here goes which contains what.

classes – compiled Java classes. We can find some Axis specific classes here.
conf – axis.xml (Axis configuration file)
lib – All necessary Axis2 libraries (JARs)
modules – Don’t ask me. Even I’m not sure. Did I mention that I’m also a beginner? 🙂
services – All web service archives & services.list

Coding: What we need to write?

1. The service provider. A Java class.
2. service.xml. The web services descriptor.
3. build.xml. To build and deploy the web services archive.
4. The service consumer. Another Java class to consume the services offered

And we are good to go now. Let’s make our hands dirty with some Java code. This is very simple and kudos to Axis2.

The service provider:

public class SampleService
  public WsOutput doSomething(WsInput anInput)
    WsOutput anOutput = new WsOutput();
    anOutput.setResponseString("I did some thing to " + anInput.getName());
    return (anOutput);

WsInput is a sample input class to demonstrate that we can have more complex IO is possible. Similarly WsOutput is the output class. Instead of using WsInput / WsOutput for IO, we can use normal data types like integer, float, string etc too.

public class WsInput implements Serializable
 private String name;
 public void setName(String name)
 { = name;

 public String getName()
   return (;

public class WsOutput implements Serializable
 private String responseString;

 public void setResponseString(String response)
   this.responseString = response;
 public String getResponseString()
   return (this.responseString);

We have the Java part of the web-service ready. But that’s not enough. We need to deploy the web-service as an AAR – Axis Archive – file. An AAR  is just another zip file (like a JAR) with a funky extension :D. The AAR should contain the class files along with the services.xml – web service descriptor.

Web service descriptor: services.xml

<service name="SampleWs" scope="application">
 <description>Subin's sample webs service</description>
 <messagereceiver mep=""
 <messagereceiver mep=""
 <operation name="doSomething" />
<parameter name="ServiceClass"></parameter>

The services.xml is the place where we define the details of the services being offered.

Build file: build.xml

<project name="my.webservice.test" default="build.aar">
<property name="deploy.dir" value=".../webapps/axis2.war/WEB-INF/services" />
<property name="" value="subinws.aar" />

 <target name="build.aar">
   <javac srcdir="src" destdir="bin" />
   <echo>Copying services.xml to bin</echo>
   <copy file="META-INF/services.xml"
       tofile="bin/META-INF/services.xml" overwrite="true"/>
   <jar basedir="bin" destfile="${}" />
   <echo>Deleting services.xml from bin</echo>
   <delete dir="bin/META-INF" />
   <copy file="${}" tofile="${deploy.dir}/${}"  />


So I hope we have an AAR ready, which is copied to the services directory. But we’ve not done yet. Now we have to make an entry in services/services.list file. Just insert the name of the ARR file at the end of it – in this case “subinws.aar”.

Please wait for part 2.