Axis 2 web service Tutorial to Create and Deploy a Web Service

Axis 2 web service Tutorial

In this tutorial we will create and deploy a web service using Axis 2

What is Apache Axis 2?

Apache Axis 2 is a core engine for Web services that is a successor of the Apache Axis Soap Project. It is a highly flexible, secured platform for Web Services.

Axis2 Web Service HelloWorld Example.

We will develop a simple Web service example and deploy on the Axis2 engine. To do this first Axis2 should be downloaded and installed into Tomcat. From Axis download we should extract the axis2.war file and place it in the webapps folder of Tomcat. This webservice will return a simple message to the client.

Folder Structure for the HelloWorld Web Service

A folder structure as below can be created

Development of the Axis2 Web Service

There are two approaches to develop webservices.

1) We can start with a wsdl file and develop the code from it (Contract first approach)
2) We can start with the code (Code first approach) . This way is mostly used.
We will try the second approach here for the example.

Steps to Follow

1) Develop the java class (Service class).
2) Develop the services.xml (service descriptor)
3) Compile the java class and create the aar file for the webservice.

Step 1.

Creating the Web Service class

Step 2.

Creating the services.xml for the Web Service

HelloWorld is the Service

org.apache.axis2.rpc.receivers.RPCMessageReceiver is the message receiver class.
The method exposed by the webservice is messge
Step 3.

Creating the aar file of the Web Service

We use javac to generate the HelloWorld.class file.
Then we go to the C:\HelloWorld\webservice directory and do jar cvf HelloWorld.aar * to get the aar file

C:\HelloWorld\webservice>jar cvf HelloWorld.aar *

Deploying the Axis2 Web Service

Place the aar file in the %CATALINA_HOME%\webapps\axis2\WEB-INF\services directory.

Where %CATALINA_HOME% is the home directory of tomcat

After starting Tomcat we visit the http://localhost:8080/axis2/services/listServices

on clicking the HelloWorld hyperlink we get the below output for the wsdl file

The url of the wsdl is http://localhost:8080/axis2/services/HelloWorld?wsdl

Output of http://localhost:8080/axis2/services/HelloWorld?wsdl

We have created a webservice called HelloWorld and deployed it in Tomcat.

Creating the Client Code for the Webservice

Here we will write the Web service client code for the HelloWorld Service we had created and call the web service.

Setting the Path and CLASSPATH for Axis.

1) We Create a environment variable called AXIS2_HOME and point to the home directory of Axis binary download.

2) Set the Path Environment to point to the AXIS2_HOME\bin directory.
3) Set te CLASSPATH environment to point to AXIS2_HOME\lib\*

After creating these configurations the wsdl2java.bat will be available for use in the command line to generate the webservice client code.

Creating the client code for the webservice usig wsdl2java.bat

By using the wsdl2java command we generate all the client code java files.

Below are the java files generated in the client folder