Interview Questions

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];
            if (v == 0)
                ordArr[k] = m;

        for (int i = 0; i < ordArr.length && ordArr[i] != 0; i++)
            sb.append((char) ordArr[i] + "" + ascArr[ordArr[i]]);

        return sb.toString();

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


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


1. Create src directory inside the Project Home. Place the 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,



Place the AxisWebService.aar file inside /repository/services

Now the deployed webservice will be available in

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

Consider the following 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

    public static void main(String[] args)
        RPCServiceClient serviceClient = null;;
            serviceClient = new RPCServiceClient();
        catch (AxisFault e1)

        //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();

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

        // Provide the EndPoint Reference available in the wsdl ,<soap12:address> tag.      
        EndpointReference targetEPR = new EndpointReference(

        //provide the action name available in the wsdl, under <wsdl:operation > tag.
        QName opName = new QName("",

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

            Object[] response = serviceClient.invokeBlocking(opName, opSetArgs,
        catch (AxisFault e)

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.

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.