
This tutorial is intended for Java developers who are looking forward for developing their first SOAP based web service application. Theoretical knowledge of Web Services (including SOAP & WSDL) , practical knowledge of Eclipse IDE & ANT is a prerequisite for understanding this tutorial.
Tools required : Java (JDK & JRE), Eclipse 3.5, Axis2 1.5
I will be using Axis2, which is one of the widely used web service/SOAP engine for deploying the below created sample web service component. I assume you already have Java & Eclipse installed in your system.
Steps for creating & running your first SOAP based web service
1) Install Axis2 : The latest version of Axis2 can be downloaded from here. Its available for download in two different formats – Standard & WAR distributions. I will be making use of the Standard binary distribution in this example. However, WAR distribution can be used instead, if you want to embed the Axis2 engine in a servlet container like Tomcat.
After downloading Axis2, to complete the installation, ‘AXIS2_HOME‘ environment variable needs to be pointed to the Axis2 home directory(eg., c:\Axis2-1.5).
2) Create a new Java project : Create a new Java project titled ‘SampleWS’ inside your Eclipse. Create a new java class called ‘SampleService’ inside the ‘com.techsagar.ws’ package. Add the below code as part of this class.
package com.techsagar.ws;
public class SampleService {
public String getOSName() {
return System.getProperty("os.name");
}
}
We will be exposing the ‘getOSName()’ method which is returning the name of the operating system, as a web service.
3) Configure the web service methods : Create a new ‘services.xml’ file in the project path & add the following contents
<serviceGroup> <service name="SampleService"> <Description> Sample Web 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" locked="false"> com.techsagar.ws.SampleService </parameter> </service> </serviceGroup>
In the above xml, we are supposed to declare all the methods which we want to expose as web services.
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="SampleWS" default="all" basedir=".">
<description>
Builds the Sample Web Service component.
</description>
<!-- Reads the system environment variables and stores them in properties prefixed with "env" -->
<property environment="env" />
<!-- Information about the application -->
<property name="app.title" value="Sample Web Service" />
<!-- Common directory locations -->
<property name="DeploymentDir" value="${env.AXIS2_HOME}/repository/services" />
<property name="SampleWSDir" value="../SampleWS" />
<property name="classesSampleWS" value="./classes/SampleWS" />
<target name="clean">
<delete dir="${classesSampleWS}" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${classesSampleWS}" />
<echo message="Compiling Sample Web Service ..." />
<javac destdir="${classesSampleWS}" debug="true" deprecation="false">
<src path="${SampleWSDir}/src" />
<include name="**/*.java" />
</javac>
<echo message="Completed Sample Web Service compiling." />
</target>
<target name="all" depends="compile">
<mkdir dir="${classesSampleWS}/META-INF" />
<copy file="${SampleWSDir}/services.xml" todir="${classesSampleWS}/META-INF" />
<zip destfile="${DeploymentDir}/samplews.aar" filesonly="false" whenempty="skip" basedir="${classesSampleWS}" />
<echo message="Completed packaging sample service" />
</target>
</project>
Right click on the build file and select ‘Run As – Ant Build’. The ant script will create an archive file named ‘samplews.aar’ inside the Axis2 deployment directory (%AXIS2_HOME%/repository/services).
5) start the Axis2 runtime : Axis2 service can be started by running the ‘axis2server.bat’ present within the ‘%AXIS2_HOME%/bin’ directory. Thats it, your web service will be up & running. You can access the web service end point (WSDL) by pointing your browser to the ‘http://localhost:8080/axis2/services/SampleService?wsdl‘ URL. The web service methods can be tested by creating a proxy of the service using a language of your choice.
The above created sample web service project is available for download from here. Let me know, if you face any difficulties in between.
If you would like to make a comment, please fill out the form below.
Thank you for posting this. I found another article which use eclipse with Axis2.
Create Web Service in Java Using Apache Axis2 and Eclipse
Hi Prasanna, Thanks for a clean & easy tutorial.
I had adoubt, hope you can help.
Does the procedure remain same, when the Web service takes input parameters.?
I followed your tutorial and created a sample Web Service. It is working fine for the methods which do not take in parameters(I tested by creating one more service method). But, when it comes to methods which takes parameters (like, addTwoNum(int, int) ), I am getting “org.apache.axis2.AxisFault: namespace mismatch require”.
Also, It would be of great help, if you can post a blog about consuming the web service using a Java application.
Thanks,
Roopesh.
I have downloaded you example and run this in soap ui .But it is not working . it is giving the following error
soapenv:Client
java.lang.AbstractMethodError: org.apache.axis2.rpc.receivers.RPCMessageReceiver.receive(Lorg/apache/axis2/context/MessageContext;)V
org.apache.axis2.AxisFault: java.lang.AbstractMethodError: org.apache.axis2.rpc.receivers.RPCMessageReceiver.receive(Lorg/apache/axis2/context/MessageContext;)V
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:178)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.AbstractMethodError: org.apache.axis2.rpc.receivers.RPCMessageReceiver.receive(Lorg/apache/axis2/context/MessageContext;)V
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:144)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:279)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:116)
… 14 more
Hi,
I have a question here. Do you think that we can expose two methods as services in the same class?
Thanks.