Interview Questions

Axis2 - Simple Web Service Example

In this section, let us see how to create a Simple Web Service using Axis-2 Web Services Engine.

Lets create a simple Java class which has only one operation "getName()".

SimpleWebService Class

package in.techdive.axis2.samples;

public class SimpleWebService
{
        public String getName(String p_name) throws Exception
        {
                return p_name;
        }
}

Create a XML file named services.xml file as follows,

services.xml

<service name="SimpleWebService">
   
    <description>
        Simple WebService Demo
    </description>
   
    <parameter name="ServiceClass">in.techdive.axis2.samples.SimpleWebService</parameter>
   
    <operation name="getName">
                <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>

</service>

Note:
1. Create src directory inside the Project Home. Place the SimpleWebService.java inside src\in\techdive\axis2\samples directory
2. Place the services.xml inside src\in\techdive\axis2\samples\META-INF directory.

Web Service Deployment:

Create an Axis archive(aar) file as SimpleWebService.aar. This file should contain the contents as below,

SimpleWebService.aar

in\techdive\axis2\samples\SimpleWebService.class
META-INF\services.xml

Place the SimpleWebService.aar file inside <AXIS2_HOME>/repository/services

Testing Web Service:

Now, its the time to test the deployed Web Service.

Start the Axis2 Server by running the <AXIS2_HOME>/bin/axis2server.bat. Once the server starts successfully, open the below the url in the browser.

http://localhost:8080/axis2/services/SimpleWebService?wsdl

This will display the WDSL content of the Service exposed. This means that the Service is deployed properly.

Download any open source SOAP UI Tool for testing our Web Service. Create the Request SOAP message and send it to the above URL. This SOAP Request will be received by the Axis engine and it will be sent to the SimpleWebService class for business logic processing and the Response SOAP message will be sent to the Client.

Request SOAP Message

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sam="http://samples.axis2.techdive.in">
   <soap:Header/>
   <soap:Body>
      <sam:getName>
         <sam:args0>Techdive.in</sam:args0>
      </sam:getName>
   </soap:Body>
</soap:Envelope>

Response SOAP Message

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <ns:getNameResponse xmlns:ns="http://samples.axis2.techdive.in">
         <ns:return>Techdive.in</ns:return>
      </ns:getNameResponse>
   </soapenv:Body>
</soapenv:Envelope>

Once the SOAP Request is sent to the above URL, the method getName() in SimpleWebService class will get executed and returns the response in XML format.