Monday, March 9, 2009

Creating Web Services With Apache Axis

The Environment Variables

Before you start, you have to set up some environment variables. You can set them from Windows System Properties or using the MS-DOS batch file (.bat)like this.

set AXIS_HOME=C:\apache-tomcat\webapps\axis\WEB-INF
set AXIS_LIB=%AXIS_HOME%\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\activation.jar;%AXIS_LIB%\mail.jar;%AXIS_LIB%\xmlsec.jar

If you are using Linux, you can use the following executable shell script (.sh) to set those environment variables.

#!/bin/sh
AXIS_HOME="/usr/local/apache-tomcat/webapps/axis/WEB-INF"; export AXIS_HOME;
AXIS_LIB="$AXIS_HOME/lib"; export AXIS_LIB;
AXISCLASSPATH="$AXIS_LIB/axis.jar:$AXIS_LIB/commons-discovery-0.2.jar:$AXIS_LIB/commons-logging-1.0.4.jar:$AXIS_LIB/jaxrpc.jar:$AXIS_LIB/log4j-1.2.8.jar:$AXIS_LIB/saaj.jar:$AXIS_LIB/wsdl4j-1.5.1.jar:$AXIS_LIB/activation.jar:$AXIS_LIB/mail.jar:$AXIS_LIB/xmlsec.jar"; export AXISCLASSPATH;

The Web Service

To create a web service with Apache Axis, you have to put all your code under webapps/axis/WEB-INF/classes folder. To make your project seems more organize, you can create one folder for each project. In this example I will create a folder named hello to store my files. And, this is the source code of helloService.java.

package hello;

public class helloService{
    public static String greeting(String name){
        return "Hello, " + name + "!";
    }
}

Compile it with this command.

javac helloService.java

Deploying Web Service

After compiling your web service, you need to deploy it. Before deploying it, you need to create a file called deploy.wsdd.

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="helloService" provider="java:RPC">
        <parameter name="className" value="hello.helloService"/>
        <parameter name="allowedMethods" value="*"/>
    </service>
</deployment>

Now, you can deploy it with this command (Don't forget to set the required environment variables before deploying it).

java -classpath %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd

Or with this command if you are using Linux.

java -classpath $AXISCLASSPATH org.apache.axis.client.AdminClient deploy.wsdd

Open the address http://localhost:8080/axis/services/helloService?wsdl with your web browser. This is the WSDL document of your helloService web service. If you see this, it means you have succesfully created your web service with Apache Axis.


Undeploying Web Service

To undeploy your web service, you have to create a file named undeploy.wsdd.

<undeployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="helloService" />
</undeployment>

Undeploy it with this command (Once again, don't forget to set the required environment variables before undeploying it).

java -classpath %AXISCLASSPATH% org.apache.axis.client.AdminClient undeploy.wsdd

Or with this command if you are using Linux.

java -classpath $AXISCLASSPATH org.apache.axis.client.AdminClient undeploy.wsdd

1 comment:

Ravi said...

Hi Bogeyman,
Where to put the deploy.wsdd file