210 likes | 343 Views
Creating a Web Service Using Apache Axis2. CSCE826 –Michael N. Huhns Guest speaker: Laura Zavala ( zavalagu@engr.sc.edu). Axis2…. Is an engine for Web services Is an implementation of the SOAP ("Simple Object Access Protocol") submission to W3C
E N D
Creating a Web Service Using Apache Axis2 CSCE826–Michael N. HuhnsGuest speaker: Laura Zavala (zavalagu@engr.sc.edu)
Axis2… • Is an engine for Web services • Is an implementation of the SOAP ("Simple Object Access Protocol") submission to W3C • not only supports SOAP 1.1 and SOAP 1.2, but also REST Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Creating 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Get 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 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Creating 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Generating a WSDL file from a Java class public class WeightConverter { public double kgtopounds (double kg){ return kg*2.20462262; } public double poundstokg (double pounds){ return pounds/2.20462262; }} classpath schema target namespace java -cp . WeightConverter.java java2wsdl -cp . -tn weightconverter -stn weightconverter -cn WeightConverter target namespace class name Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Generating 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 our 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling 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"); } Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling 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 kg*2.20462262;} Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling the server skeletal code • But as you can see, the input and output parameters are different than in our original WeightConverter class public double kgtopounds (double kg){ return kg*2.20462262;} 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"); } Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Filling 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(kg*2.20462262); return result; } Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Compiling the code • Download the compile.bat script: http://www.cse.sc.edu/~zavalagu/axis2/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) Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Deploying and running the service • Once you 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Running your service • Run axis server with: axis2server • This will start an 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 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
A SOAP request for your service <?xml version="1.0" encoding="UTF-8" ?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <kgtopounds xmlns="weightconverter"> <param0>50</param0> </kgtopounds> </soapenv:Body> </soapenv:Envelope> Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
A SOAP request for your service: TCPMon Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
An HTTP request for your service http://localhost:8080/axis2/services/WeightConverter/kgtopounds?param0=50 Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
An HTTP request for your service Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns
Code generator plug-in for Eclipse • http://ws.apache.org/axis2/tools/1_3/eclipse/wsdl2java-plugin.html Service-Oriented Computing: Semantics, Processes, Agents - Munindar Singh and Michael Huhns