Title: Creating a Web Service Using Apache Axis2
1Creating a Web Service Using Apache Axis2
- CSCE 526 Service-Oriented Computing
2Axis2
- Is an engine for Web services
- Is an implementation of the SOAP ("Simple Object
Access Protocol") submission to W3C - Supports not only SOAP 1.1 and SOAP 1.2, but also
REST
3Creating Services and Clients
- Axis2 makes these tasks very easy, as it comes
with code generator tools that simplify the work
for you - Basically, you can use the code generator to
- Generate code to access an existing service as a
client - Generate a WSDL file from java source code
- Generate the skeletal code to implement a service
from a WSDL file describing the service
functionality
4Get your environment ready for working with Axis2
- Download and install a Java Development Kit (JDK)
release (version 1.4 or later) - Set an environment variable JAVA_HOME to the
pathname of the directory into which you
installed the JDK release - Download Apache Axis2 (currently at
http//ws.apache.org/axis2/download/1_4_1/download
.html and extract it to a target directory - Set the AXIS2_HOME environment variable to point
to the target directory in the previous step - Add the AXIS2_HOME\bin directory to your PATH
environment variable - Download Apache Ant and extract it to a target
directory - Set the ANT_HOME environment variable to point to
the target directory in the previous step - Add the ANT_HOME\bin directory to your PATH
environment variable
5Creating Services
JAVA class containing the functionality (public
methods) you want to expose in a service
Skeletal code to implement the service on the
server side
java2wsdl
wsdl2java
WSDL file describing the service functionality
6Generating a WSDL file from a Java class
public class WeightConverter public
double kgtopounds (double kg) return
kg2.20462262 public double poundstokg
(double pounds) return
pounds/2.20462262
schema target namespace
classpath
java -cp . WeightConverter.java
java2wsdl -cp . -tn weightconverter -stn
weightconverter -cn WeightConverter
target namespace
class name
7Generating the service skeletal code from the
WSDL file
service descriptor
wsdl2java -ss -sd -uri WeightConverter.wsdl
server side
- A src directory is created with the source code
for the server side files - A resources directory is created with the WSDL
file for the service and a service descriptor
(services.xml) file - A build.xml file is created in the current
directory, which will be used to create the ws
deployment file
8Filling the server skeletal code
- Now you need to fill WeightConverterSkeleton.java
with the necessary business logic. You can copy
the functionality from your original java file,
though you need to change it a little to match
the new data types
9Filling the server skeletal code
- public weightconverter.KgtopoundsResponse
kgtopounds (weightconverter.Kgtopounds
param0) //Todo fill this with the necessary
business logic throw new - java.lang.UnsupportedOperationException("Please
implement " this.getClass().getName()
"kgtopounds")
10Filling the server skeletal code
- You need to replace the throw instruction (which
is the default functionality for the case in
which not implementation is provided before
deploying the service) by your original
functionality, which was
- public double kgtopounds (double kg)
return kg2.20462262
11Filling the server skeletal code
- But as you can see, the input and output
parameters are different than in our original
WeightConverter class
public weightconverter.KgtopoundsResponse
kgtopounds (weightconverter.Kgtopounds
param0) //Todo fill this with the necessary
business logic throw new java.lang.Unsupport
edOperationException("Please implement "
this.getClass().getName() "kgtopounds")
- public double kgtopounds (double kg)
return kg2.20462262
12Filling the server skeletal code
- The original kgtopounds method takes a double and
returns a double. The new kgtopounds method takes
a weightconverter.Kgtopounds object and returns a
weightconverter.KgtopoundsResponse object - If you dive into the generated Kgtopounds and
KgtopoundsResponse data type files you will find
the methods public double getParam0() and public
void set_return(double param) in them
13Filling the server skeletal code
- You can use those methods to get the double value
of the input parameter, then perform your
original operations, and finally set the double
value of the output. The result is as follows
- public weightconverter.KgtopoundsResponse
kgtopounds (weightconverter.Kgtopounds
param0) double kg param0.getParam0()
weightconverter.KgtopoundsResponse result
- new weightconverter.KgtopoundsResponse()
result.set_return(kg2.20462262)
return result
14Compiling the Code
- Download the compile.bat script
- http//www.cse.sc.edu/huhns/csce526/compile.bat
- into the src directory and use it to compile
the source code files - compile weightService\.java
- This script calls the "javac" command after
adding the classpath for Axis2 dependent
libraries (.jar files present in your
AXIS2_HOME/lib)
15Deploying and Running the Service
- Once you have compiled the server files, use the
ant command (run it where the build.xml file is)
to generate the deployment files for the service - ant jar.server
- A build directory will be created
- Copy the build\lib\WeightConverter.aar file to
the AXIS2_HOME/repository/services directory
16Running your Service
- Run the Axis2 server with
- axis2server
- This will start a standalone Axis2 server using
the AXIS2_HOME/repository directory as the Axis2
repository and the AXIS2_HOME/conf/axis2.xml as
the Axis2 configuration file
17A SOAP request for your service
lt?xml version"1.0" encoding"UTF-8"
?gt ltsoapenvEnvelope xmlnssoapenv"http//sch
emas.xmlsoap.org/soap/envelope/"
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce"gt ltsoapenvBodygt ltkgtopounds
xmlns"weightconverter"gt ltparam0gt50lt/param0gt lt
/kgtopoundsgt lt/soapenvBodygt lt/soapenvEnvelopegt
18A SOAP request for your service TCPMon
19An HTTP request for your service
http//localhost8080/axis2/services/WeightConvert
er/kgtopounds?param050
20An HTTP request for your service
21Code and Archive Generator Plug-Ins for Eclipse
http//ws.apache.org/axis2/tools/index.html