Pages

Sunday, March 25, 2007

Step by step Web Services with Axis2 Tutorial in Java

It was very hard for me to get a good tutorial on developing web services with axis2 on the internet. After some effort I was able to deploy a axis2 web service. I hope this tutorial below will help you for bringing up the web service using axis2 a lot easier.(This shows sync/blocking call client).
The tutorial shows how to get your web service up as quickly as possible, with out giving intrinsic details. For more details regarding any aspect of the tutorial, the axis2 documents will assist you.

My Configuration:

a) AXIS2 1.1.1

b) JDK 1.5

c) ANT 1.7.0

d) TOMCAT 5.5


Before proceeding, please read the installation doc of axis2 so that it will work with tomcat with no problems. The below steps assumes that the axis2 is configured with tomcat and is running in the tomcat container. You can test the same by visiting http://localhost:8080/axis2/ url, and then clicking on the validate link.


Step 0: Set the following paths in your system.(This step is very important)
a) AXIS2_HOME = "your axis2 installation(unzipped) directory"
b) JAVA_HOME = "your jdk installation path"
c) PATH = "add ant, axis2/bin to this"

Step 1: Create your service class.

In my case:

package axis2.adb;
public class MyService {
public String sayWelcomeToAxis2(String userName){
return "Hi " + userName + "\nWelcome to the world of Axis2";
}
}

You can actually use an interface to define the service contracts, which your service will implement. But, as i said, the goal is to set the webservice up as fast and in as simple way as possible.

Step 2: Generate the WSDL file using the command:
java2wsdl -cp ./bin -cn axis2.adb.MyService -of ./META-INF/MyService.wsdl

here -cp gives the classpath, in my case the class files are present in 'bin' folder. -cn is the class name with package and -of is the output file.

Step 3: Generate the Skeleton using the below command. I will be using the ADB(Axis data binding). You can use XMLBean or JiBx or AXIOM also.

wsdl2java -uri ./META-INF/MyService.wsdl -p axis2.adb -d adb -s -ss -sd -ssi -o .

here uri provides you with the WSDL file path, -p specifies the package, -d gives the data binding(ADB in my case), -s sync call, -ss creates the server side code (skeleton and related files), -sd creates a service descriptor (services.xml file), -ssi switch creates an interface for the service skeleton.

Step 4: Put the business logic inside the skeleton.

package axis2.adb;
import axis2.adb.MyService;
import axis2.xsd.SayWelcomeToAxis2Response;
/**
* MyServiceSkeleton java skeleton for the axisService
*/
public class MyServiceSkeleton implements MyServiceSkeletonInterface {
MyService myService = new MyService();
/**
* Auto generated method signature
*
* @param param2
*
*/
public axis2.xsd.SayWelcomeToAxis2Response sayWelcomeToAxis2(
axis2.xsd.SayWelcomeToAxis2 param2)
{
//create the response obj
SayWelcomeToAxis2Response res = new SayWelcomeToAxis2Response();
//set the response payload
res.set_return(myService.sayWelcomeToAxis2(param2.getUserName()));
// return the response
return res;
}
}

Step 5: Create the Client Stub using the command below.

wsdl2java -uri ./resources/MyService.wsdl -p axis2.adb.client -d adb -s -o .

Step 6: Code the Client using the generated Stub.

package axis2.adb.client;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
public class MyServiceClient {
public static void main(String[] args) {
try {
//create the stub
MyServiceStub stub = new MyServiceStub("http://localhost:8080/axis2/services/MyService");
// webservice call happens inside this method
sayWelcome(stub);
} catch (AxisFault e) {
e.printStackTrace();
}
}
private static void sayWelcome(MyServiceStub stub) {
//create the request payload
MyServiceStub.SayWelcomeToAxis2 req = new MyServiceStub.SayWelcomeToAxis2();
//set the parameter
req.setUserName("SacroSanctBlood");
MyServiceStub.SayWelcomeToAxis2Response res = null;
try {
//get the response(This example shows a blocking call)
res = stub.sayWelcomeToAxis2(req);
//show the output
System.out.println(res.get_return());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}


Step 7: Create ".aar" file which has to be deployed inside axis2 using the below command.
The build.xml file is created in Step 3.

ant build.xml

Step 8: Deploy.

A new folder build is created inside the current directory. The MyService.aar file is present in \build\lib. Upload this using the axis2 admin page.
Type, http://localhost:8080/axis2/. Click on Administration. Login using the username/password, by default it is admin/axis2. (present in %AXIS2_HOME%/conf/axis2.xml) Use the Upload Service tool present in the left side of page. Upload the MyService.aar.

Step 9: Test the service.

type http://localhost:8080/axis2/services/MyService?wsdl in addressbarr. you should get the WSDL file.

Step 10: Execute the client.

java MyServiceClient


output :

Hi SacroSanctBlood

Welcome to the world of Axis2

Hope, this tutorial was use full enough.
Next time, will try to put up the async/non-blocking version of the web service.

7 comments:

  1. Anonymous12:54 PM

    hi, a pretty good tutorial!!

    ReplyDelete
  2. Anonymous4:46 AM

    hi thanks for such a simple & clear tutorial..

    ReplyDelete
  3. Anonymous9:25 AM

    Hey shreyas, can u help me out on this..
    I have to generate client stubs from a given wsdl file, then i have to automate the process of sending and receiving requests from the web server, pretty much using JAX-WS alone.
    A post on this would be very clear..
    Thank you.

    ReplyDelete
  4. Hi,

    I was not clear about what you wanted, so here goes, from what i understood from your question.

    As in Step 5, give your wsdl URI or location to generate stubs. Code your client, it is in this eg, a public static void main and run.

    If you are talking about the JAX-WS, there is one more tutorial I have written, which uses eclipse, if you understand it, then you can do it with out eclipse @:
    http://sacrosanctblood.blogspot.com/2008/12/tutorial-to-use-eclipse-with-jax-ws-for.html

    If you are asking me about automation, then, you will have to give me a lil more detail of what exactly is your usecase.

    Thanks,
    Shreyas

    ReplyDelete
  5. Hi there,
    First of all, thanks for the tutorial, it's really hard to find information and examples of the matter.

    Now the question: Everything works perfect until i try the "ant build.xml" command, i get the following error:

    BUILD FAILED
    Target "build.xml" does not exist in the project "null".

    I really don't know what it means, any help would be greatly apprectiated.

    ReplyDelete
  6. Anonymous8:30 AM

    Hi,
    fuckn thank u dude! You almost saved my life with this clear and simple tutorial

    ReplyDelete
  7. Miki, when you say ant build.xml, ant is trying to search a target by name build.xml. Please give just "ant" or "ant -f build.xml".
    I think you are not completely aware of ant, please go through ant tutorials on web. Its a powerful tool!

    ReplyDelete