Axis2- Web Service Client

In this article lets discuss about how to access a web service using axis2.

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

AxisWebService Class

package in.techdive.axis2.samples;

public class AxisWebService
{

    /*
     * This method returns a string containing number of occurrence of each character from the input string. It is case sensitive.
     */

    public String getUniqueCharCount(String s)
    {

        int[] ascArr = new int[123];
        int[] ordArr = new int[s.length()];
        StringBuilder sb = new StringBuilder();
        int k = 0;

        for (int i = 0; i < s.length(); i++)
        {
            char m = s.charAt(i);
            int v = ascArr[m];
            ascArr[m]++;
            if (v == 0)
            {
                ordArr[k] = m;
                k++;
            }
        }

        for (int i = 0; i < ordArr.length && ordArr[i] != 0; i++)
        {
            //System.out.print((char)ordArr[i]+""+ascArr[ordArr[i]]);
            sb.append((char) ordArr[i] + "" + ascArr[ordArr[i]]);
        }

        return sb.toString();
    }
}

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

services.xml

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

</service>

Note:
1. Create src directory inside the Project Home. Place the AxisWebService.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 AxisWebService.aar. This file should contain the contents as below,

AxisWebService.aar

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

Place the AxisWebService.aar file inside /repository/services

Now the deployed webservice will be available in
http://localhost:8080/axis2/services/AxisWebService?wsdl

Ok! Deployment over!!! Its time to create a client in axis2 to test the deployed service.

Consider the following Axis2Client.java to access the webservice.

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;

public class Axis2Client
{
    //http://localhost:8080/axis2/services/AxisWebService?wsdl

    public static void main(String[] args)
    {
        RPCServiceClient serviceClient = null;;
        try
        {
            serviceClient = new RPCServiceClient();
        }
        catch (AxisFault e1)
        {
            e1.printStackTrace();
        }

        //if the web service to be accessed is authenticated then set the userName and passWord in the below authenticator object
        HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator();
        authenticator.setUsername("<userName>");
        authenticator.setPassword("<passWord>");

        Options options = serviceClient.getOptions();
        options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);

        // Provide the EndPoint Reference available in the wsdl ,<soap12:address> tag.      
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/AxisWebService.AxisWebServiceHttpSo...");

        //provide the action name available in the wsdl, under <wsdl:operation > tag.
        options.setAction("urn:getUniqueCharCount");
        options.setTo(targetEPR);
        QName opName = new QName("http://samples.axis2.techdive.in",
                "getUniqueCharCount");

        Object[] opSetArgs = new Object[] { "Blackberry" };
        Class[] returnTypes = new Class[] { String.class };

        try
        {
            Object[] response = serviceClient.invokeBlocking(opName, opSetArgs,
                    returnTypes);
            System.out.println(response[0]);
        }
        catch (AxisFault e)
        {
            e.printStackTrace();
        }
    }
}

The above class uses RPCServiceClient api of axis2 framework to access the web service.

We need to provide the following to the api to access the web service.

1.) User Credentials for web service authentication, if required
2.) Endpoint Reference from wsdl
3.) webservice method name to be called
4.) namespace, provided in the wsdl
5.) arguments for the webservice methods.

Have a look at the output of the given webservice.

log4j:WARN No appenders could be found for logger (org.apache.axis2.util.Loader).
log4j:WARN Please initialize the log4j system properly.
B1l1a1c1k1b1e1r2y1

In this way we can call a web service using axis2 framework.
Note: You need to include the necessary *.jar files for executing the client code.

Technology: 

Search