Welcome to Apache Axis2 Tutorial. Recently I was trying to upgrade my Apache Axis2 skills from 1.3 to latest version 1.5.4 and I was not able to find out any tutorial that is self-explanatory and covering the latest release. So it forms the basis of my post for Axis2 Web Services Tutorial.
This tutorial is intended for Java programmers who are interested in developing and deploying Web Services using Apache Axis2.
The scope of this tutorial is to use Axis2 for creating web services and invoking the web service using a Java client program and testing web service using Soap UI tool. Basic understanding of Java, Web Services, XML, Ant, and application server (Tomcat) is required to understand the tutorial with ease.
Download the latest version of Apache Tomcat latest version. As of now, the latest version is 7.0.8 and that is what I am using for this tutorial. It requires minimum Java Version 1.6, so make sure it’s installed in your system. If Java 6 is not installed in your system, you should download and install it first from Java SE Downloads. Download the Tomcat Core zip (apache-tomcat-7.0.8.zip) and unzip it to install it on your system. Set the JAVA_HOME environment variable to start and stop the server.
Download Apache Axis2 1.5.4 Binary Distribution zip from Apache Axis2 – Releases. This step is required to create axis2.war that will be deployed to tomcat and to get the axis2 libraries to be used in projects.
Unzip the Axis2 binary distribution zip into any convenient directory. Go to axis2-1.5.4/webapp directory and run the “ant create.war” command to create the axis2.war deployment in the axis2-1.5.4/dist directory. If you don’t have Apache Ant installed, you can download and install it from Apache Ant – Binary Distributions. Please note that I was facing some issue with axis2.war downloaded from War Distribution. Later on, I found out that few jars are missing in the axis2 War Distribution. War Distribution contains only 58 jars whereas Binary Distribution contains 63 jars. (I am feeling lazy to find out, which jars are missing.)
$ ant create.war
Buildfile: build.xml
init:
[mkdir] Created dir: /Users/pankaj/Downloads/axis2-1.5.4/dist/temp
[copy] Copying 59 files to /Users/pankaj/Downloads/axis2-1.5.4/dist/temp
prepare.repo:
[copy] Copying 9 files to /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF
[mkdir] Created dir: /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF/conf
[copy] Copying 1 file to /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF/conf
create.war:
[war] Building war: /Users/pankaj/Downloads/axis2-1.5.4/dist/axis2.war
[delete] Deleting directory /Users/pankaj/Downloads/axis2-1.5.4/dist/temp
BUILD SUCCESSFUL
Total time: 2 seconds
Deploy the axis2.war in the tomcat application server by copying it in tomcat webapps directory. You may need to restart the server if it’s not supporting hot deployment.
Go to https://localhost:8080/axis2/ and click on Validate link. If the Happy Axis page is coming with GREEN color then it means that axis2 is successfully deployed. Our system setup is ready now and we can proceed for creating Axis2 web services.
For creating Axis2 Web Service archive, we need following:
The project structure will look like the below image. Don’t get confused with the content inside build folder. They will be created when we will execute the build.xml ant file.
MyService.java: Implementation class that will be exposed as Axis2 web service.
package com.journaldev.ws;
import com.journaldev.bean.MyBean;
public class MyService {
public String getData(String input) {
return "Hi" + input;
}
public MyBean getObjectData(MyBean myBean) {
String name = myBean.getName();
int id = myBean.getId();
myBean.setId(id + 100);
myBean.setName("Output: " + name);
return myBean;
}
}
MyBean.java: Java Bean class that is input and output of getObjectData operation in web service.
package com.journaldev.bean;
import java.io.Serializable;
public class MyBean implements Serializable {
private static final long serialVersionUID = -1129402159048345204L;
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
services.xml: Axis2 web service related parameters are part of this xml file. ServiceClass parameter specifies the class that will be exposed as web service. The other important parameters are targetNamespace and schemaNamespace. services.xml
<service name="MyService" scope="application" targetNamespace="https://journaldev.com/">
<description>
MyService
</description>
<messageReceivers>
<messageReceiver mep="https://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="https://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<schema schemaNamespace="https://journaldev.com/xsd"/>
<parameter name="ServiceClass">com.journaldev.ws.MyService</parameter>
</service>
build.xml: Ant build file for performing Axis2 tasks. There are three targets defined whose details are:
build.xml
<project name="AxisWSImplementation" basedir="." default="generate.service">
<property environment="env"/>
<property name="build.dir" value="build"/>
<path id="axis2.classpath">
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile.service">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<mkdir dir="${build.dir}/resources"/>
<!--First let's compile the classes-->
<javac debug="on"
fork="true"
destdir="${build.dir}/classes"
srcdir="${basedir}/src"
classpathref="axis2.classpath">
</javac>
</target>
<target name="generate.wsdl" depends="compile.service">
<taskdef name="java2wsdl"
classname="org.apache.ws.java2wsdl.Java2WSDLTask"
classpathref="axis2.classpath"/>
<java2wsdl className="com.journaldev.ws.MyService"
outputLocation="${build.dir}"
targetNamespace="https://journaldev.com/"
schemaTargetNamespace="https://journaldev.com/xsd">
<classpath>
<pathelement path="${axis2.classpath}"/>
<pathelement location="${build.dir}/classes"/>
</classpath>
</java2wsdl>
</target>
<target name="generate.service" depends="compile.service">
<copy toDir="${build.dir}/classes" failonerror="false">
<fileset dir="${basedir}/resources">
<include name="**/*.xml"/>
</fileset>
</copy>
<jar destfile="${build.dir}/MyService.aar">
<fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
</jar>
</target>
<target name="generate.client" depends="compile.service">
<taskdef name="wsdl2java"
classname="org.apache.axis2.tool.ant.AntCodegenTask"
classpathref="axis2.classpath"/>
<wsdl2java
wsdlfilename="${build.dir}/MyService.wsdl"
output="${build.dir}/resources" />
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
Copy the MyService.aar in ~apache-tomcat-7.0.8/webapps/axis2/WEB-INF/services directory. Axis2 supports hot deployment of services, so you don’t need to restart the server. Check your service deployment on ListServices page (https://localhost:8080/axis2/services/listServices). MyService should be listed there with two operations.
After deploying the service, first of all we need to test it. Here I am using SoapUI that is one of the best tools for Web Service Testing. If you don’t have it, you can download it from their website and install it easily. Steps for Testing using SoapUI
Now we will proceed with the last task of invoking the web service using Axis2 stub classes.
Axis2ClientUsingStubsFromAnt Code
package com.journaldev.ws.client;
import java.rmi.RemoteException;
import com.journaldev.MyServiceStub;
import com.journaldev.MyServiceStub.GetData;
import com.journaldev.MyServiceStub.GetDataResponse;
import com.journaldev.MyServiceStub.GetObjectData;
import com.journaldev.MyServiceStub.GetObjectDataResponse;
import com.journaldev.MyServiceStub.MyBean;
/**
*
* @author Pankaj - www.journaldev.com This class will invoke Axis2 web service
* operations using Stub classes
*
*/
public class Axis2ClientUsingStubsFromAnt {
/**
* END_POINT is the web service endpoint
*/
private final static String END_POINT = "https://localhost:8080/axis2/services/MyService";
public static void main(String[] args) throws RemoteException {
System.out.println("START");
// Create the Stub Object by passing the Web Service Endpoint URL
MyServiceStub stub = new MyServiceStub(END_POINT);
// Creating an input object for the getData operation
GetData getDataInput = new GetData();
// Setting the input part in the getData input object
getDataInput.setInput("PANKAJ");
// invoking the getData operation
GetDataResponse getDataOutput = stub.getData(getDataInput);
// get_return method returns the web service output object. Here its
// String, so we can
// directly print the returned value
System.out.println("Output:" + getDataOutput.get_return());
// Creating input object for the getObjectData operation
GetObjectData getObjectDataInput = new GetObjectData();
MyBean myBean = new MyBean();
myBean.setId(1);
myBean.setName("KUMAR");
// Setting the input part in the getObjectData input object
getObjectDataInput.setMyBean(myBean);
// invoking the getObjectData operation
GetObjectDataResponse getObjectDataOutput = stub
.getObjectData(getObjectDataInput);
// Get the MyBean object from the response object
MyBean myBeanOutput = getObjectDataOutput.get_return();
// Print the myBeanOutput values to check that web service operations
// are getting invoked
System.out.println("ID:" + myBeanOutput.getId() + "NAME:"
+ myBeanOutput.getName());
System.out.println("DONE");
}
}
Execute the Axis2ClientUsingStubsFromAnt class to invoke the web service. Output of the above program is:
START
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
Output:HiPANKAJ
ID:101NAME:Output: KUMAR
DONE
If you find the tutorial helpful in understanding Axis2 and getting started with it, please share your thoughts in the comment section. And yeah, don’t forget to share it across with others. Your two clicks and 5 seconds time can help someone else to learn Axis2 easily. :)
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hi Pankaj, This is a nice article. Can you please guide how to implement the service payload with attachment (external to the body with mime support ) in axis2. Thanks
- Ranjan
1. Security policy namespace cannot be null Exception Throwing when we send the message to the Web service listener and we are using Axis2 1.7.9 version.
- Senthil Kumar
Hi Pankaj, Please share link for source code of import com.journaldev.MyServiceStub; and import com.journaldev.MyServiceCallbackHandler
- Praful Anand
Thank you sir, your article is still helpful in 2019 :D
- Zakaria Kaoukab
this can be implemente in windows ?
- joshua
you don’t reply to problem, but only to compliments… getting issue as below: \build.xml:33: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver cannot be cast to org.apache.axis2.engine.MessageReceiver at org.apache.ws.java2wsdl.Java2WSDLBuilder.(Java2WSDLBuilder.java:109)
- Jiten
After run
https://localhost:8080/axis2/services/
it displays only: Deployed services Version Available operations getVersion not completed web service in win7- Shilendra
Hye Pankash, Thank you for your great tutorial. Do you have any tutorial regarding how to get raw request XML made by client? I create my web services using Axis2 and now I am stuck and dont know how to get and put in log file the request XML made by client -Miss Suri
- Suri
Hi Pankaj, Do you have tutorial on SOAP webservices? with examples.
- Jayanti Rode
Hello all, i am getting the below error when i am starting the tomcat server, can someone help on it. “[ERROR] The MyService.aar service, which is not valid, caused The services.xml file cannot be found for the service: C:\apache-tomcat-7.0.68\webapps\axis2\WEB-INF\services\MyService.aar” Thanks in advance, Dinesh.
- dinesh