Goal
Develop and deploy web service in Apache Tomcat by using Axis2
1.2/1.3.
Prerequisite:
(1) Install JDK, Tomcat and Axis2.
Keys:
(a) Versions of these packages must match.
(b) JDK is recommended. Though JRE works most of the time, some features of the additional libraries/packages may not work well.
(2) Configure JDK. I think the most important part is to set the environment.
There are already lots of tutorials around the internet. Typically, you should set environment variables : JAVA_HOME and CLASSPATH.
(3) Configure Tomcat
Tomcat must be run on JVM. So path of JVM needs to be configured correctly.
(4) Deploy Axis2
Axis2 is deployed in Tomcat. You can put the
axis2.war file to
webapps directory of Tomcat. Tomcat automatically unextracts and deploys it.
In addition, you should set
AXIS2_HOME environment variable to point to the location of axis2.
Procedure
Now, let's start to write our service prototype.
(1) Define interface in Java. My sample interface:
File Add.java
package test;
interface Add{
int addoperation( int op1, int op2 ) ;
}
(2) Now, compile Add.java to .class file. Command: javac Add.java
(3) Then, we will generate WSDL document based on the interface defined above. The tool is Java2WSDL which is included in Axis2 package. My command used to generate WSDL document is:
Java2WSDL -cn test.Add The newly generated WSDL document is
Add.wsdl.
Note: (1) Java2WSDL generates WSDL document based on .class file, not .java source file. So step (2) is necessary.
(2) Java2WSDL command must be invoked in right directory. You must obey that package name must match
the directory hierarchy. In my case,directory tree is:
D:/apps/demo/test/Add.java. When I invoke Java2WSDL,
current working directory is
D:/apps/demo/.
(4) Use WSDL2Java (included in Axis2 package) to generate stub Java functions based on WSDL document.There are two parts: client side and server side.
Detailed information about WSDL2Java/Java2WSDL is here:
http://ws.apache.org/axis/java/user-guide.html#UsingWSDLWithAxis(4.1)
Server side
First you need to copy the generated
Add.wsdl to the machine where service is to be located. In my case, the
Add.wsdl is stored at
D:/apps/demo/service.
(4.1.1) Generate stub functions
Again, WSDL2Java is used. Command is:
WSDL2Java -uri C:/apps/demo/test/Add.wsdl -p test -d adb -s -ss -sd -ssi
After excution of this command, directory tree looks like:
D:/apps/demo/service/
Add.wsdl
resource
Add.wsdl
services.xml
src
test
AddMessageReceiverInOut.java
Addoperation.java
AddoperationResponse.java
AddSkeleton.java
AddSkeletonInterface.java
ExtensionMapper.java
build.xml
(4.1.2) Add functionality of your service to generated methods
File AddSkeleton.java contains code directly related to your web service implementation. Initial content of AddSkeleton.java is:
/**
* AddSkeleton.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 1.3 Built on : Aug 10, 2007 (04:45:47 LKT)
*/
package test;
/**
* AddSkeleton java skeleton for the axisService
*/
public class AddSkeleton implements AddSkeletonInterface {
/**
* Auto generated method signature
* @param addoperation0
*/
public test.AddoperationResponse addoperation(
test.Addoperation addoperation0) {
//TODO : fill this with the necessary business logic
throw new java.lang.UnsupportedOperationException("Please implement " +
this.getClass().getName() + "#addoperation");
}
}
I modify AddSkeleton.java to add my web service implementation. New content is:
package test;
public class AddSkeleton implements AddSkeletonInterface {
public test.AddoperationResponse addoperation(
test.Addoperation addoperation0) {
//first, get two parameters
int op1 = addoperation0.getParam0();
int op2 = addoperation0.getParam1();
int sum = op1 + op2; //calculate summation
AddoperationResponse resp = new AddoperationResponse();//construct response object
resp.set_return( sum );//set the return value
return resp;
}
}
(4.1.3) Build a service
Change directory to D:/apps/demo/service/, then execute the following command:
ant jar.serverAfter execution, directory tree looks like:
D:/apps/demo/service/
Add.wsdl
resource
Add.wsdl
services.xml
src
test
AddMessageReceiverInOut.java
Addoperation.java
AddoperationResponse.java
AddSkeleton.java
AddSkeletonInterface.java
ExtensionMapper.java
build.xml
build
classes
META-INF
Add.wsdl
services.xml
test
some .class files
lib
Add.aar
Note:
Add.aar is the the archive.
(4.1.4) Deploy the service
Copy Add.arr to the "services" directory (Tomcat_directory/webapps/axis2/WEB-INF/services) of deployed axis2. Axis2 provides a web-based interface to manage the deployed services. You can use that interface to upload your .arr file.
(4.2)
Client side
(4.2.1) Generate stub functions
My command used to generate client-side Web service stub functions is:
WSDL2Java -uri Add.wsdl -p test.axis2.add -d adb -sAfter execution of this command, directory tree should look like:
D:/apps/demo/
Add.wsdl
test
Add.java
Add.class
src
test
AddStub.java
build.xml
The file
AddStub.java contains the newly generated client-side stub functions.
(4.2.2) develop your code to use web service
(4.2.2.1) Write source code to invoke remote web service
Create a new file called
Client.java (Actually, you can use any name you like) in the directory
D:/apps/demo/src/test/.
Content of the file: package test;
import java.io.*;
import test.AddStub.Addoperation;
import test.AddStub.AddoperationResponse;
public class Client {
public static int op1 = 0;
public static int op2 = 0;
public static void main(java.lang.String[] args) {
try {
AddStub stub = new AddStub("http://localhost:8080/axis2/services/Add");
BufferedReader in=new BufferedReader(new InputStreamReader System.in));
System.out.println("Please input the first operand:");
String input = in.readLine();
op1 = Integer.parseInt( input );
System.out.println("Please input the second operand:");
input = in.readLine();
op2 = Integer.parseInt( input );
add(stub);
} catch (Exception e) {
e.printStackTrace();
System.out.println("\n\n\n");
}
}
/* invoke the 'add' web service */
public static int add(AddStub stub) {
try {
Addoperation req = new Addoperation();
req.setParam0(op1);
req.setParam1(op2);
AddoperationResponse resp = new AddoperationResponse();
resp = stub.addoperation(req);//invoke the web service
System.out.println("done");
System.out.println(op1 + "+"+ op2 + " is " + resp.get_return());
return resp.get_return();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\n\n\n");
}
return 0;
}
}
Note: In axis2 1.4, the generated code is different from code generated by axis2 1.3. In axis2 1.3, parameters of a request are set using
setParam0,
setParam1,...
In axis2 1.4, parameters of a request are set using setter functions of those properties, e.g.
setName,
setId.
Also the method to get return value is different. There may be some other changes I don't know, so I encourage to consult official Axis2 document.
(4.2.2.2) compile the source file and execute it.
Change your current working directory to:
C:/apps/demo/, then execute following command:
ant jar.clientThen, a new directory "
build" is created. Now the directory looks like:
D:/apps/demo/
Add.wsdl
test
Add.java
Add.class
src
test
AddStub.java
build.xml
build
classes
test
some .class files
lib
Add-test-client.jar
Now, you can use java command to execute the program.
Remeber: first you must set CLASSPATH to include axis2 .jar libraries and newly created
Add-test-client.jar. One alternative way is to use option
-cp of command
java. Command to execute your program is:
java test.Client #with CLASSPATH being set
or
java -cp <axis2_jars>:./Add-test-client.jar test.Client #without CLASSPATH being set
To relieve programmers from these fussy stuff, axis2 provides a tool axis2.bat/axis2.sh. This script automatically set CLASSPATH to include all the .jar files of axis2. How can the script know where your axis2 is installed? Ha, you must create a new environment variable called AXIS2_HOME which contains the path where axis2 is installed. However, the newly created Add-test-client.jar is not included in the classpath by the axis2.bat/axis2.sh script. So you still need to add it to CLASSPATH. Execute your program using command:
axis2.sh -cp ./Add-test-client.jar test.Client
In fact, I wrote my own script to ease invocation of that program. Now, I don't need to set long CLASSPATH or input long command line with -cp option.