Creating a Simple Web Service using Apache Axis 1.4

Create two projects namely AxisServerExample and AxisClient example.
Download the Axis installation and add the jars in the lib folder of the installation to the lib folder of the Server project. Implement MattizAxisServerImpl.java in the server project.
MattizAxisServerImpl class has the following code:

package com.mattiz.ws.axis.server.impl;

public class MattizAxisServerImpl {
/**
 *
 * @param person
 * @return message
 */
	public String returnMessage(String person){

		return "Hallo "+person+" to the World of Axis!";
	}

}

Run the following two commands from the command prompt to use the java2wsdl utility to create the wsdl from the java class.

set PROJECT_HOME=I:/juno-ws/mvn_custom_plugin/AxisServerExample (This is the location of my axis server project)

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar;%PROJECT_HOME%/bin"  org.apache.axis.wsdl.Java2WSDL -o %PROJECT_HOME%/resources/mattiz.wsdl -l "http://localhost:8080/axis/services/MattizAxisTest" -n  "urn:mattiz" -p"com.mattiz.ws.axis.server.impl" "urn:mattiz" com.mattiz.ws.axis.server.impl.MattizAxisServerImpl

This will generate the wsdl file from MattizAxisServerImpl class in the resources folder of the Server project.

Run the following from the command prompt to use the wsdl2java utility to create the java client classes and deploy.wsdd from the wsdl file.

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar" org.apache.axis.wsdl.WSDL2Java -o %PROJECT_HOME%/src -p com.mattiz.axis.request -s %PROJECT_HOME%/resources/mattiz.wsdl

deploy.wsdd is created in com.mattiz.axis.request folder along with web serice client stub classes in the Server project.

Copy contents of axis installation webapps folder to tomcat. Modify web.xml by uncommenting this line:

 <!--
  <servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
  </servlet-mapping>
 -->

Start tomcat and run the following from the command prompt to use the Apache Admin Client to generate your server-config file (wsdd).

java -cp "%PROJECT_HOME%/lib/axis-ant.jar;%PROJECT_HOME%/lib/axis.jar;%PROJECT_HOME%/lib/commons-discovery-0.2.jar;%PROJECT_HOME%/lib/commons-logging-1.0.4.jar;%PROJECT_HOME%/lib/jaxrpc.jar;%PROJECT_HOME%/lib/log4j-1.2.8.jar;%PROJECT_HOME%/lib/saaj.jar;%PROJECT_HOME%/lib/wsdl4j-1.5.1.jar" org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService %PROJECT_HOME%/src/com/mattiz/axis/request/deploy.wsdd

server-config.wsdd is created in the axis deployment in tomcat in the WEB-INF folder. Copy this to resources folder under Server project.

You are free to delete the axis installation in webapps of tomcat after this step.

Copy web.xml from the axis distribution at axis-1_4/webapps/axis/WEB-INF to WEB-INF under Server project. No changes are required to this file.
Modify returnMessage method in AxisServerExample/src/com/mattiz/axis/request/MattizAxisTestSoapBindingImpl.java

which was generated with the stubs, to look like this:

    public java.lang.String returnMessage(java.lang.String person) throws java.rmi.RemoteException {
        return new MattizAxisServerImpl().returnMessage(person);
    }

Run the following build.xml which creates the server side web service war file – mattiz.war.

<?xml version="1.0"?>
<project name="mattiz" default="war" basedir=".">
	<path id="project.class.path">
		<fileset dir="lib">
			<include name="*.jar" />
		</fileset>
	</path>
	<target name="clean">
		 <delete failonerror="true" includeemptydirs="true">
            <fileset dir="target/classes" includes="**/*" />
            <fileset dir="target" includes="**/*" />
        </delete>
	</target>
	<target name="compile" depends="clean">
		<mkdir dir="target" />
		<mkdir dir="target/classes" />
		<javac srcdir="src" destdir="target/classes" optimize="off"
			classpathref="project.class.path" />
	</target>
	<target name="war" depends="compile">
		<copy file="resources/server-config.wsdd" todir="target/classes" />
		<war destfile="target/mattiz.war" webxml="WEB-INF/web.xml">
			<classes dir="target/classes">
				<include name="**/*.*"/>
			</classes>
			<lib dir="lib">
				<include name="**/*.jar" />
			</lib>
		</war>
	</target>
</project>

Copy mattiz.war to tomcat and start tomcat server.

Run the following url on a browser to view the wsdl file. If the web service is successfully created you will see a xml version of the wsdl file.

http://localhost:8080/mattiz/services/MattizAxisTest?wsdl

Copy contents of com.mattiz.axis.request package to the Client project. Exclude MattizAxisTestSoapBindingImpl class.

Add activation.jar and javamail jars to the classpath of Client project in addition to the axis jars in Server project’s lib folder.

Create MattizWebServiceConsumer.java thus:

package com.mattiz.axis.request;

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;

public class MattizWebServiceConsumer {

	public static void main(String[] args) {
		try {
			String endpoint = "http://localhost:8080/mattiz/services/MattizAxisTest";

			MattizAxisServerImplServiceLocator srvLoc = new MattizAxisServerImplServiceLocator();
			srvLoc.setEndpointAddress(new javax.xml.namespace.QName(
					"urn:mattiz", "MattizAxisTest"), endpoint);
			MattizAxisServerImpl serviceimpl = srvLoc.getMattizAxisTest();

			((javax.xml.rpc.Stub) serviceimpl)._setProperty(
					"javax.xml.rpc.session.maintain", Boolean.TRUE);
			String person = "Matty";

			String ret = serviceimpl.returnMessage(person);
			System.out.println("Message " + ret);

		} catch (ServiceException e) {
			// TODO Auto-generated catch blockS
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Run MattizWebServiceConsumer.java as standalone.

You see the following messsage on the console when the web service java client calls the server:

Message Hallo Matty to the World of Axis!
client

server

The Axis server example can be downloaded here here
The Axis client example can be downloaded here

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: