Monday, November 2, 2009

Creating a simple web service using Axis2

This post will briefly outline how I implemented a sample web service using Axis2 POJOs. First you need to setup a project directory for you. It should be structed like this:


rootdir/build.xml
rootdir/src/META-INF
rootdir/src/META-INF/services.xml
rootdir/src/mywebservice
rootdir/src/mywebservice/ExampleService.java


Implement a simple java (POJO) with one setter and one getter. This will be the
server side of the web service.

ExampleService.java

package mywebservice;

public class ExampleService
{
float someValue = 42;

public float getSomeValue()
{
return someValue;
}

public void setSomeValue(float someValue)
{
this.someValue += someValue;
}
}


Steps to build and deploy locally,
  1. Build the web service using by running ant in the root directory.
  2. Copy the whole build/MyWebService directory into /var/lib/tomcat6/webapps/axis2/WEB-INF/services/MyWebService
  3. Check that http://localhost:8080/axis2/services/listServices lists your new web service.
  4. You can look at the WSDL document by clicking on your services.

That's it.
In my next post I will show how to do the client side using gSOAP and C/C++.

Related reads:
- Axis2 POJO Tutorial
- services.xml description

Reference services.xml:
<service name="ExampleService" scope="application">
<description>My example service</description>

<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

</messageReceivers>

<parameter name="ServiceClass">
mywebservice.ExampleService
</parameter>
</service>

Reference ant file, store this as build.xml:
<project name="MyWebService" basedir="." default="generate.service">
<property name="service.name" value="MyWebService">
<property name="dest.dir" value="build">
<property name="dest.dir.classes" value="${dest.dir}/${service.name}">
<property name="dest.dir.lib" value="${dest.dir}/lib">
<property name="axis2.home" value="../../axis2-1.5">
<property name="repository.path" value="${axis2.home}/repository">

<path id="build.class.path">
<fileset dir="${axis2.home}/lib">
<include name="*.jar">
</fileset>
</path>

<target name="prepare">
<mkdir dir="${dest.dir}">
<mkdir dir="${dest.dir}/lib">
<mkdir dir="${dest.dir.classes}">
<mkdir dir="${dest.dir.classes}/META-INF">
</target>

<target name="clean">
<delete dir="${dest.dir}">
<delete dir="src" includes="sample/pojo/stub/**">
</target>

<target name="generate.service" depends="clean,prepare">

<copy file="src/META-INF/services.xml" tofile="${dest.dir.classes}/META-INF/services.xml" overwrite="true">

<javac listfiles="yes" srcdir="src" destdir="${dest.dir.classes}" includes="mywebservice/**">
<classpath refid="build.class.path">
</javac>

<jar basedir="${dest.dir.classes}" destfile="${dest.dir}/${service.name}.aar">
<copy file="${dest.dir}/${service.name}.aar" tofile="${repository.path}/services/${service.name}.aar" overwrite="true">

</target>
</project>

No comments:

Post a Comment