This tutorial is intended for Java developers who are looking forward for developing SOAP Web Service in Java . The sample SOAP web service developed here makes use of Axis2 as the web service engine. Theoretical knowledge of SOAP Web Service and practical knowledge of Eclipse IDE , ANT will be a prerequisite for understanding this tutorial.
Tools required :
Java 7 (JDK & JRE), Eclipse 3.5, Axis2 1.5
I will be using Axis2, which is one of the widely used SOAP web service engine for deploying the below created sample SOAP web service component. I assume you already have Java & Eclipse installed in your system.
Steps for creating & running your first SOAP 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 SOAP 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.
4. Package & deploy the SOAP web service component :
Create a new build.xml in the project path & add the following ant script to package the soap web service component.
<?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>
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 Axis2 Web Service will be up & running. You can access the Axis2 Web Service end point (WSDL) by pointing your browser to the ‘http://localhost:8080/axis2/services/SampleService?wsdl‘ URL. The Axis2 Web Service methods can be tested by creating a proxy of the service using a language of your choice.
The above created Axis2 Web Service project can be downloaded from the below link. Feel free to get in touch with me if you face any difficulties.