A Developer's Diary

Jan 20, 2013

Consuming a webservice using Apache Axis2

The post assumes one to be familiar with the steps to create and host a web service using Apache Axis2. If not, you can refer the following tutorial.

Configuring Apache Axis2
1. Download the Apache Axis2 Binary packages from here
2. Extract the binary package and set the variable AXIS2_HOME to point to the extracted location
3. Append %AXIS2_HOME%\bin to your path

Setting up Eclipse for Apache Axis2
1. Launch Eclipse IDE
2. Goto Windows and select Preferences.
3. Search for Web Services. You will find Axis2 Preferences
4. Select %AXIS2_HOME% folder as Axis2 Runtime location
The confirmation message shows that eclipse has configured Apache Axis2 successfully.
Generating the client stubs
Command for generating the client stubs
WSDL2Java.bat -o wsclient -p com.ws.client.stub -uri http://localhost:8080/WebService/services/EchoService?wsdl

Writing WebService client using eclipse
1. Create a new Java Project
2. Add the client stubs generated using WSDL2Java.bat command by importing the files from the local file system in your eclipse project
3. Add all the libraries present under %AXIS2_HOME%\lib in the Java Build Path of the project
4. Create a new class EchoServiceClient which makes request and processes the webservice response as shown below
5. Export the contents as a runnable jar file ws-client.jar
package com.examples.ws.client;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import com.ws.client.stub.EchoServiceStub;

public class EchoServiceClient
{
    public static void main(String[] args)
    {

        try
        {
            EchoServiceStub stub = new EchoServiceStub();
            EchoServiceStub.DoEcho request = new EchoServiceStub.DoEcho();
            request.setMessage("I am Sam!");
            EchoServiceStub.DoEchoResponse response = stub.doEcho(request);
            String message = response.get_return();
            System.out.println("Echo: " + message);
        }
        catch (AxisFault e)
        {
            e.printStackTrace();
        }
        catch (RemoteException e)
        {
            e.printStackTrace();
        }

    }
}

Output

5 comments :

Unknown said...

401 error unauthorised?

pankaj said...

Can you provide more details?

Unknown said...

Thanks for reply. Error I get connecting to an internal sharepoint site is as follows:

log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisOperation).
log4j:WARN Please initialize the log4j system properly.
org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:310)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.ws.client.stub.ListsStub.getList(ListsStub.java:8089)
at com.examples.ws.client.EchoServiceClient.main(EchoServiceClient.java:18)

pankaj said...

The message is coming from Sharepoint server. It is denying access to the webservice.
Check your sharepoint site settings if it is allowing anonymous access to the site/service.

Unknown said...

thanks. I'll give that a go. I don't have access to the central admin though but will wait until I get my own sharepoint site configured

Post a Comment