Interview Questions

Axis2 - Web Service Implementation using WSDL

In this example, Let us see how to create a Web Service implementation using WSDL.

Let us assume, we are going to perform the implementation for Student Record system. Now, lets develop the Web Service which will accept Student Roll Number and retrieves the respective Student Name. If the Roll Number is not found, then Error will be thrown.

Let us see how to achieve this in Axis-2 SOAP Engine.

Consider the following WSDL for Student processing.

Student.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:ns="http://samples.axis2.techdive.in" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://samples.axis2.techdive.in">
   
        <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://samples.axis2.techdive.in">
            <xs:element name="getStudentName">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="rollNumber" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
                       
            <xs:element name="getStudentNameResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
       
    <wsdl:message name="getStudentNameRequest">
        <wsdl:part name="parameters" element="ns:getStudentName"/>
    </wsdl:message>
       
    <wsdl:message name="getStudentNameResponse">
        <wsdl:part name="parameters" element="ns:getStudentNameResponse"/>
    </wsdl:message>
       
    <wsdl:portType name="StudentPortType">
        <wsdl:operation name="getStudentName">
            <wsdl:input message="ns:getStudentNameRequest" wsaw:Action="urn:getStudentName"/>
            <wsdl:output message="ns:getStudentNameResponse" wsaw:Action="urn:getStudentNameResponse"/>
        </wsdl:operation>
    </wsdl:portType>
       
    <wsdl:binding name="StudentSoap11Binding" type="ns:StudentPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getStudentName">
            <soap:operation soapAction="urn:getStudentName" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
       
    <wsdl:binding name="StudentSoap12Binding" type="ns:StudentPortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getStudentName">
            <soap12:operation soapAction="urn:getStudentName" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
       
    <wsdl:binding name="StudentHttpBinding" type="ns:StudentPortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="getStudentName">
            <http:operation location="Student/getStudentName"/>
            <wsdl:input>
                <mime:content type="text/xml" part="getStudentName"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="getStudentName"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
       
    <wsdl:service name="Student">
        <wsdl:port name="StudentHttpSoap11Endpoint" binding="ns:StudentSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/Student"/>
        </wsdl:port>
        <wsdl:port name="StudentHttpSoap12Endpoint" binding="ns:StudentSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/Student"/>
        </wsdl:port>
        <wsdl:port name="StudentHttpEndpoint" binding="ns:StudentHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/Student"/>
        </wsdl:port>
    </wsdl:service>
       
</wsdl:definitions>

Using the tool <AXIS2_HOME>/bin/wsdl2java.bat, Let us create the Skeleton and other supported files for this service.

Place the Student.wsdl inside <AXIS2_HOME>/bin directory and execute the below command

C:\Axis\axis2-1.5\bin>wsdl2java.bat -uri file:\\\C:/Axis/axis2-1.5/bin/Student.wsdl -p org.apache.axis2.axis2userguide -o target_directory_name -d adb -s -wv 1.5.1 -ss -sd

This will create an output named target_directory_name inside <AXIS2_HOME>/bin directory. The contents of target_directory_name are,

build.xml
resources\services.xml
resources\Student.wsdl
src\in\techdive\axis2\samples\ExtensionMapper.java
src\in\techdive\axis2\samples\GetStudentName.java
src\in\techdive\axis2\samples\GetStudentNameResponse.java
src\in\techdive\axis2\samples\skeleton\StudentMessageReceiverInOut.java
src\in\techdive\axis2\samples\skeleton\StudentSkeleton.java

In the above list, the Java file named in\techdive\axis2\samples\skeleton\StudentSkeleton.java is the one which is used to place our custom Business logic code.

Auto generated StudentSkeleton Java code:

package in.techdive.axis2.samples.skeleton;

/**
 * StudentSkeleton java skeleton for the axisService
 */

public class StudentSkeleton
{

        /**
         * Auto generated method signature
         *
         * @param getStudentName
         */


        public in.techdive.axis2.samples.GetStudentNameResponse getStudentName(
                        in.techdive.axis2.samples.GetStudentName getStudentName)
        {
               
                throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName()
                                + "#getStudentName");
        }
}

Let us add our Business logic in the above class to implement our functionality.

Custom Business Logic added in the Skeleton class:

package in.techdive.axis2.samples.skeleton;

import in.techdive.axis2.samples.GetStudentNameResponse;

import java.util.HashMap;

import org.apache.axis2.engine.AxisError;

/**
 * StudentSkeleton java skeleton for the axisService
 */

public class StudentSkeleton
{

        /* Custom Logic - Starts here */
        private static HashMap<Integer, String> studentMap      = new HashMap<Integer, String>();

        static
        {
                studentMap.put(1, "Mike");
                studentMap.put(2, "John");
                studentMap.put(3, "Jack");
        }

        /* Custom Logic - Ends here */

        /**
         * Auto generated method signature
         *
         * @param getStudentName
         */


        public in.techdive.axis2.samples.GetStudentNameResponse getStudentName(
                        in.techdive.axis2.samples.GetStudentName getStudentName)
        {
               
                /* Custom Logic - Starts here */
                int inputRollNumber = getStudentName.getRollNumber();
                String studentName = studentMap.get(inputRollNumber);

                GetStudentNameResponse response = new GetStudentNameResponse();

                if (studentName != null)
                {
                        response.set_return(studentName);
                }
                else
                {
                        throw new AxisError("Student Record Not Found");
                }

                return response;
                /* Custom Logic - Ends here */
        }

}

Place the above StudentSkeleton.java inside <AXIS2_HOME>/bin/target_directory_name/src/in/techdive/axis2/samples/skeleton directory.

Now, its time to build and test our Web Service implementation.

Build:
Execute the build.xml using ANT tool.

C:\Axis\axis2-1.5\bin>cd target_directory_name
C:\Axis\axis2-1.5\bin\target_directory_name>ant

The above ANT build, will create Student.aar file inside <AXIS2_HOME>\bin\target_directory_name\build\lib directory.

Web Service Deployment:
Place the <AXIS2_HOME>\bin\target_directory_name\build\lib\Student.aar 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/Student?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 Skeleton class for business logic processing. The Skeleton class will process the request and it will create the SOAP response message to 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:getStudentName>
         <!--Optional:-->
         <sam:rollNumber>1</sam:rollNumber>
      </sam:getStudentName>
   </soap:Body>
</soap:Envelope>

In the above SOAP Request, Student Roll Number is set as 1 and sent to the Web Service. This in turn returns the corresponding Student Name in the SOAP response as below.

Response SOAP Message:

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

If the Student Roll Number is set as 1001 (which doesn't exists in our Student System), then the SOAP Server will throw SOAP Error Message to the Client as below.

SOAP Error Message:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <soapenv:Fault>
         <soapenv:Code>
            <soapenv:Value>soapenv:Receiver</soapenv:Value>
         </soapenv:Code>
         <soapenv:Reason>
            <soapenv:Text xml:lang="en-US">Student Record Not Found</soapenv:Text>
         </soapenv:Reason>
         <soapenv:Detail/>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>