Wednesday, June 23, 2010

Webservices and Axis

These days web services are becoming so popular and used in a wide area.Putting up a web service is not something like rocket science. Basically what it does is exchanging data through the Internet in a XML based format which we call "SOAP" (Simple Object Access Protocol).SOAP is meant for exchanging data in a distributed environment, and SOAP is built on XML

So we can say SOAP is a mechanism for inter-application communication between systems written in arbitrary languages, across the Internet. SOAP usually exchanges messages over HTTP.These SOAP messages exchange structured information between SOAP systems and they consist of one or more SOAP elements inside an envelope, Headers and the SOAP Body.So here Axis is an Open Source SOAP server and client which is an Apache project.

Axis implements the JAX-RPC API, one of the standard ways to program Java services.Here we will be talking more about JWS which stands for Java Web Services.

For instance lets say ,a java class which has a method "adder" to add two supplied values.At the end of this article you will find it really easy to expose this adder as a web service.

Lets get in to the action :Now we are going to deploy a web service with Axis.

Step-1:

You need to download and install Axis.For this please follow the install.html under docs directory in your binary distribution of axis.In brief what you have to do is that , copy the axis directory inside your Axis downloaded distribution in to your current web server's webapp directory (it is recommended to have a Apache Tomcat web server)

Step-2:

Also you have to copy any XML parser in to the axis/WEB-INF/lib directory .It is also recommended that to use Apache Xerces parcer and you can download it from here.

Step-3:

Start your web server.And you can simply check whether the installation has succeeded by simply requesting for http://localhost:8080/axis .

Thats it for now.

Above steps are to setup the axis environment.But how we deploy our web service.

Lets take a look at this sample Adder class .

public class Adder {

public int adder(int a, int b) {

return a+b;
}
}

Only you have to do here is that , create this class and name it as Adder.java and copy it in to axis directory and rename it as Adder.jws.This is how your "adder" method becomes a web service under axis framework.

Lets implement a small client to connect to the web service and invoke it.Here is a simple client class for you to run and test how this works.


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

import java.net.*;

public class AdderServiceClient {
public static void main(String [] args) throws Exception {

Service service = new Service();
Call call = (Call)service.createCall();

String endpoint = "http://localhost:8080/axis/Adder.jws";
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName("getCount"));

int a = 12;
int b = 13;
int count = 0;

int total = (Integer)call.invoke(new Object [] {new Integer(a), new Integer(b)});

System.out.println("Got result : " + total);
}
}


Please note that ,what we need to specify is the URL of the jws-file and the name of the method to invoke. Of course we also need to give the two strings with the division and team names. The convention is that they must be given as an Object array.

Now you will see the result as "Got result : 25"

This is a very simple demonstration of how JWS works.

Regards
/subash