Saturday, January 24, 2009

Developing and debugging web services using axis, eclipse and Tomcat

www.mallutalk.com


Introduction
Recently I was reading through SOA and I wanted to develop few services using Axis. Since I was already comfortable with eclipse as j2ee development IDE (I use eclipse 3.4 – Ganymede for development purposes). When I started with axis, I wanted to use eclipse for development of web-services too. I struggled couple of hours googling to find best way to write, debug, deploy and distribute web services.

Initially I used “Axis2 Codegen Wizard” and “Axis2 Service Archiver”. Initially I got few error dialogs like “java.lang.reflect.InvocationTargetException”





Though I found out way to fix it by adding few jars to the lib folder of the pluggin and modifying the pluggin.xml, it was not really useful as it just generate the required files and there was no possibility I could find to debus the services. Overall, I thought there could be a better way and I googled and finally I would an acceptable way to do development and debug. These are information I put together from my own experience and various sources from internet.

Before starting, I want to mention my development environment.


Development environment
Java – J2ee SDK 1.4.
Jre 1.5.
Eclipse 3.4 Ganymede.
Axis2 - 1.4



Create Web Service
For better clarity, I will take you through one service creation and one client creation using eclipse. For simplicity I will create a simple calculator service with 4 simple service functions.


· add
· divide
· substract
· multiply



Step 1: Create a dynamic web project.
File -> New -> Project ->


















Press Finish and you have a new dynamic web project.


Step 2: Create a simple Calculator class.



package org.bpt.Services;

/**
*
* @author Boby Thomas
*
* Axis Service Sample.
* Visit www.bobsindia.com
*
*/

public class Calculator {

public double add(double arg1, double arg2){
return arg1 + arg2;
}

public double substract(double arg1, double arg2){
return arg1 - arg2;
}

public double multiply(double arg1, double arg2){
return arg1 * arg2;
}

public double divide(double arg1, double arg2){
if(arg2 != 0)
return arg1 / arg2;
else return -1;
}
}




Step 3: Create an axis service from this class.


Right click Calculator.java
New -> Other -> Web Service






Next



Click on the link and select Axis 2.







Click OK and click Next in Web service Dialog.





Now click on Start Server and wait for few seconds.
Press Next



Finish. You have created a new axis service, deployed it and started the server now.

Check Services



To check the deployed service, check the following URL in a browser.
http://localhost:8080/CalculatorService/services/listServices



You see the following screen with the newly created service listed.



To check whether service is working properly, check the following URLs


To see the service definition, click the following link.
http://localhost:8080/CalculatorService/services/Calculator?wsdl



Add Service:

http://localhost:8080/CalculatorService/services/Calculator/add?arg1=12&arg2=13




You see an xml file containing result as below. You can change the arguments to see the service working.




Substract Service

http://localhost:8080/CalculatorService/services/Calculator/substract?arg1=12&arg2=13



You see an xml file containing result as below. You can change the arguments to see the service working.



Multiply Service

http://localhost:8080/CalculatorService/services/Calculator/multiply?arg1=12&arg2=13


You see an xml file containing result as below. You can change the arguments to see the service working.



Divide Service

http://localhost:8080/CalculatorService/services/Calculator/divide?arg1=12&arg2=13



You see an xml file containing result as below. You can change the arguments to see the service working.




Cool and easy ..isn’t it?


Creating a Java Client for the web Service

Now we will create a Java client which used the services we created now. There are many ways to create clients and I will explain one of them.

File -> New -> Project -> Dynamic Web project










Click Finish and now you have a dynamic web project “CalculatorServiceClient”.
Now select on your Calculator.java file in CalculatorService project.

File -> New -> Other -> Web Service Client




Next



Set it to Axis 2.




Add service definition URL - http://localhost:8080/CalculatorService/services/Calculator?wsdl







Select Calculator client project




Next



Keep the default setting s and press finish. You see that new classes are created for the client.




Now we will write a simple console application which uses these generated classes to access the services – CalculatorServiceClient.java.


package org.bpt.services;

import org.apache.axis2.AxisFault;

public class CalculatorServiceClient {

public static void main(String[] args) {
try {
CalculatorStub stub = new CalculatorStub();
double val = add(stub, 12,13);
System.out.println("Got response from add service. result = " +val);
} catch (AxisFault e) {
e.printStackTrace();
}

}

/* fire and forget */
public static double add(CalculatorStub stub, double arg1, double arg2) {
try {
CalculatorStub.Add req = new CalculatorStub.Add();
req.setArg1(arg1);
req.setArg2(arg2);
CalculatorStub.AddResponse res = stub.add(req);

return res.get_return();
} catch (Exception e) {
e.printStackTrace();
System.err.println("\n\n\n");
}
return -1;
}


}


Run the application and you see the result in the console. Congratulations. You have created your first web service in eclipse. You are now ready to fly to the heights. All the best for a great fly.

About Author
Visit www.bobsindia.com


www.mallutalk.com

8 comments:

  1. Nice job! great for webservice developing beginner.

    ReplyDelete
  2. dude,
    this is exactly what i was expecting , something simple and working... thumbs up...

    - sriram

    ReplyDelete
  3. Hey Its Anil...thanks a lot...u have done really good job....

    ReplyDelete
  4. Thanks for Nice Job ...Many Many Thanks
    Sachin..

    ReplyDelete
  5. Sir i create this example than run than i found a Apache error
    Requested resource not found!
    please help me
    sachin

    ReplyDelete
  6. Good Example. Simple and easy to understand the concept.. Bravo.

    Padmini

    ReplyDelete
  7. Nice article. Can you also write a small article on creating a SOAP webservice that returns an XML output and consuming such webservice.

    ReplyDelete