Java Web Services Using Apache Axis2
Java Web Services Using Apache Axis2
In this section we will learn Apache Axis2 with the help of examples. Apache Axis2 is a Web Service engine for deploying the web services. The Apache Axis2 Web services engine is available for Java and C language. I this tutorial we will learn about the Apache Axis2 for Java. The Axis 1.5 is the latest release at the time of writing of this tutorial. We will use Apache Axis2 1.5 for developing and testing our example code. The Axis2 1.5 requires JDK 1.5 or above to run. You can deploy the axis2.war on tomcat or any other servlet container and start deploying and testing the applications. The Apache Axis2 was introduced in August 2004 Summit in Colombo, Sri Lanka. Now Apache Axis2 is more flexible, efficient and configurable compared to the Axis 1.x. Let's get started with Apache Axis2 1. Introduction to Apache Axis2 This section introduces you with the Apache Axis2 Engine. More about Apache Axis2 Tutorial.
2. Apache Axis2 on Tomcat The Apache Tomcat server can be used to run the Axis2 engine and deploy the Web services.
3. Downloading and Installing Apache Axis2 In this example we will download and install the Axis2 on tomcat server.
4. Apache Axis2 Hello World Example In this example we will download and install the Axis2 on tomcat server.
5. Testing Apache Axis2 Hello World Example In this example we will develop client application and test it.
6. WSDL2Java ant tool In this section we will learn how to use SWDL2java tool from ant build.xml file.
After modification, Apache Axis2 offers some key features given below: Speed - Axis2 has greater speed than Apache Axis1 as it uses its own object model and StAX (Streaming API for XML).
Low memory footprint - For greater flexibility, Axis2 was designed with low memory footprint. AXIOM - AXIOM is the lightweight object model of Axis2 used for enhanced, superior performant and developer convenient message processing service.
Hot Deployment - A new service can be added in Axis2 without having to shut down the server. For doing this, users need to simply drop the required Web service archive into the services directory in the repository. The deployment model will automatically deploy the service.
Asynchronous Web services - Axis2 now supports asynchronous Web services & asynchronous Web services invocation using non-blocking clients and transports.
MEP Support - With its in-built support for basic Message Exchange Patterns (MEPs), Axis2 is quite capable in message handling.
Flexibility - Axis2 is more flexible now as it enables developers to insert extensions into the engine for custom header processing, system management, and many more features.
Stability - Axis2 is more stable than its previous versions as it defines a more rigid set of interfaces that does not need to change frequently.
Component-Oriented Deployment - You can easily define reusable networks of Handlers to implement common patterns of processing for your applications, or to distribute to partners.
Transport Framework - We have a clean and simple abstraction for integrating and using Transports (i.e., senders and listeners for SOAP over various protocols such as SMTP, FTP, message-oriented middleware, etc), and the core of the engine is completely transportindependent.
WSDL support - Axis2 supports WSDL allowing users to easily build stubs to access remote services, and also to automatically export machine-readable descriptions of your deployed services from Axis2.
Add-ons - Several Web services specifications have been incorporated including WSS4J for security (Apache Rampart), Sandesha for reliable messaging, Kandula which is an encapsulation of WS-Coordination, WS-AtomicTransaction and WS-BusinessActivity.
Composition and Extensibility - Axis2 is supported with improved composition and extensibility allow to add support for new WS-* specifications in a simple and clean manner. They are however not hot deployable as they change the overall behavior of the system.
Limitations of Axis2 ADB does not support unwrapping of response messages (coming in 1.2) JSR 181/183 Annotation support (coming in 1.2) JaxMe and JAXBRI data binding support is experimental Simple Axis Server does not support POX/REST RPC Message Receiver does not validate request against the schema (AXIS2-1943) Axis2 Idea plugin shows random behavior when XMLBeans is used.
Tomcat is one of the most used Servlet container. It is open source and free.
Let's see how we can install the Apache Axis2 engine on Tomcat server Step 1: Download, install and configure JDK 6 or above on your computer Step 2: Download and install Apache Tomcat on your computer. Step 3: If you want to develop and test the Web services from Eclipse IDE, then download it from the eclipse website. Read in detail at Axis2 Eclipse plugin Tutorial.
Step 4: Now you can download and install the Apache Axis2 engine on Tomcat server. Read it in detail atDownloading and Installing Apache Axis2.
In this section we will download and install the Apache Axis 2 engine for testing the application.
Then download the "Documents", "Standard Binary Distribution" and "WAR (Web Archive) Distribution" versions. Create a new directory in c: or d: and copy the downloaded files (axis2-1.5.1-bin.zip, axis2-1.5.1-docs.zip and axis2-1.5.1-war.zip) there. Now extract the files using zip tool such as winzip or winrar. Installing Axis2 engine on Tomcat Download and install Tomcat 6 or above. Now to install the Axis2 engine copy axis2.war into Tomcat's webapps directory. Now start the Tomcat server. Testing the Axis2 Installation You can test the Axis2 engine by opening the browser and typing http://localhost:8080/axis2 . Your browser will display the Axis2 home page and it should look like:
Axis2 Web Admin Module Now click on the "Administration" link and then login as User: admin Password: axis2 After successful login you should be able to access the Administration panel. The administration panel will look like following screen shot:
In this section we have downloaded and installed the Apache Axis2 engine on Tomcat server.
In this section we will develop a simple Hello World Web service and then deploy on the Axis2 engine.
first
approach) or from the Service code(Code first approach). Most developer prefers the code first
approach. We will start with the source and create the Web service. Here are the steps involved in creating the new Web services with code first approach in Apache Axis2:
1. Develop the Service Class 2. Develop the service descriptor e.g. services.xml 3. Compile and Service class and create the Web services achieve file (.aar)
theMETA-INF directory of the service achieve. You can have multiple services in the services.xml file. The <service>...</service> tag is used to configure the Web service. <service > <!-- Configuration of the service --> </service> Here is the services.xml file for our application: <service> <parameter name="ServiceClass" locked="false">net.roseindia.HelloWorldService</parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </operation> </service>
Our service class is net.roseindia.HelloWorldService and the message receiver isorg.apache.axis2.rpc.receivers.RPCMessageReceiver. The operation we are exposing is sayHello.
E:\Axis2Tutorial\Examples\HelloWorld\webservice>javac net/roseindia/*.java
Create the service achieve using the following command:
E:\Axis2Tutorial\Examples\HelloWorld\webservice>jar cvf HelloWorldService.aar * added manifest ignoring entry META-INF/ adding: META-INF/services.xml(in = 242) (out= 162)(deflated 33%) adding: net/(in = 0) (out= 0)(stored 0%) adding: net/roseindia/(in = 0) (out= 0)(stored 0%) adding: net/roseindia/HelloWorldService.class(in = 489) (out= 321)(deflated 34%)
E:\Axis2Tutorial\Examples\HelloWorld\webservice>
You can view the WSDL file at http://localhost:8080/axis2/services/HelloWorldService?wsdl . Here is the WSDL file: <?xml version="1.0" encoding="UTF-8" ?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2 /xsd"xmlns:ns="http://roseindia.net" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"xmlns :http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema"xmln s:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/s oap/"xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://roseindia. net"> - <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://roseindia.net"> - <xs:element name="sayHello"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="args0" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="sayHelloResponse"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> - <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="ns:sayHello" /> </wsdl:message> - <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="ns:sayHelloResponse" /> </wsdl:message> - <wsdl:portType name="HelloWorldServicePortType"> - <wsdl:operation name="sayHello"> <wsdl:input message="ns:sayHelloRequest" wsaw:Action="urn:sayHello" /> <wsdl:output message="ns:sayHelloResponse" wsaw:Action="urn:sayHelloResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="HelloWorldServiceSoap11Binding"type="ns:HelloWorldServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="sayHello"> <soap:operation soapAction="urn:sayHello" style="document" /> - <wsdl:input> <soap:body use="literal" />
</wsdl:input> - <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="HelloWorldServiceSoap12Binding"type="ns:HelloWorldServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="sayHello"> <soap12:operation soapAction="urn:sayHello" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="HelloWorldServiceHttpBinding" type="ns:HelloWorldServicePortType"> <http:binding verb="POST" /> - <wsdl:operation name="sayHello"> <http:operation location="HelloWorldService/sayHello" /> - <wsdl:input> <mime:content type="text/xml" part="sayHello" /> </wsdl:input> - <wsdl:output> <mime:content type="text/xml" part="sayHello" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="HelloWorldService"> - <wsdl:port name="HelloWorldServiceHttpSoap11Endpoint " binding="ns:HelloWorldServiceSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/HelloWorldService .HelloWorldServiceHttpSoap11Endpoint/ " /> </wsdl:port>
- <wsdl:port name="HelloWorldServiceHttpSoap12Endpoint"binding="ns:HelloWorldServiceSoap12Binding "> <soap12:address location="http://localhost:8080/axis2/services/HelloWorldService .HelloWorldServiceHttpSoap12Endpoint/ " /> </wsdl:port> - <wsdl:port name="HelloWorldServiceHttpEndpoint"binding="ns:HelloWorldServiceHttpBinding"> <http:address location="http://localhost:8080/axis2/services/HelloWorldService .HelloWorldServiceHttpEndpoint/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
You have successfully deployed the Hello World service on Tomcat server. In the next section we will create the client and test the application. Download code
In this section we will develop client code example to access the Hello World Web service developed in the last section.
E:\>cd E:\Axis2Tutorial\Examples\HelloWorld\client
E:\Axis2Tutorial\Examples\HelloWorld\client>WSDL2Java.bat -uri http://localhost: 8080/axis2/services/HelloWorldService?wsdl -o client Using AXIS2_HOME: E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1 Using JAVA_HOME: E:\JDK\jdk1.6.0_03 Retrieving document at 'http://localhost:8080/axis2/services/HelloWorldService?wsdl'. The above command will generate a) HelloWorldServiceStub.java and b) HelloWorldServiceCallbackHandler.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory. Now run cd to client/src directory.
Now go to E:\Axis2Tutorial\Examples\HelloWorld\client\client\src directory and with the help of javac command compile the code. E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.java Note: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. To run the client type following command: java net/roseindia/Test Here is the output: E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.java Note: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>java net/roseindia/Test log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService). log4j:WARN Please initialize the log4j system properly.
In this last section we generated the client code manually and developed the client to test the Hello World Web service.
- <fileset dir="E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1"> <include name="**/*.jar" /> </fileset> </path> - <target name="gen"> <taskdef name="axis2wsdl2java" classname="org.apache.axis2.tool.ant.AntCodegenTask"classpathref="axis2.classpath" /> <axis2wsdl2java wsdlfilename="http://localhost:8080/axis2/services/HelloWorldService?wsdl "output="src/s ervices" /> </target> </project> To run the wsdl2java from ant go to E:\Axis2Tutorial\Examples\AntWSDL2Java\project and then type ant at dos prompt. Here is the output of the dos prompt: E:\Axis2Tutorial\Examples\AntWSDL2Java\project>ant Buildfile: build.xml
gen: [axis2-wsdl2java] Retrieving document at 'http://localhost:8080/axis2/services/HelloWorldService?wsdl'. [axis2-wsdl2java] log4j:WARN No appenders could be found for logger (org.apache.axis2.description.WSDL11ToAllAxisServicesBuilder). [axis2-wsdl2java] log4j:WARN Please initialize the log4j system properly.
BUILD SUCCESSFUL Total time: 3 seconds Now you can check the src/services to view the generated files. You can use the generated files to call the Web service. Download code End