Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
134 views

Java Web Services Using Apache Axis2

This document provides an overview of Apache Axis2, an open-source framework for developing and deploying web services. It discusses how Axis2 is more flexible and efficient than previous versions. It also covers how to install and configure Axis2 on the Tomcat application server, and provides a simple example of creating a "Hello World" web service using Axis2.

Uploaded by

kdorairajsg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Java Web Services Using Apache Axis2

This document provides an overview of Apache Axis2, an open-source framework for developing and deploying web services. It discusses how Axis2 is more flexible and efficient than previous versions. It also covers how to install and configure Axis2 on the Tomcat application server, and provides a simple example of creating a "Hello World" web service using Axis2.

Uploaded by

kdorairajsg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Apache Axis2 - Apache Axis2 Tutorial

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.

In this section introduces you with the Apache Axis2 framework.

Apache Axis2 Introduction


Axis2: An Introduction In this section introduces you with the Apache Axis2 framework. Apache Axis2 is a core engine for Web services. It is a modified version of widely used Apache Axis Simple Object Access Protocol (SOAP) stake and re-designed and re-write engine of Web Service Definition Language (WSDL), which is more flexibility and power. There are two implementations of the Apache Axis2 Web services engine - Apache Axis2/Java and Apache Axis2/C. Due to being a modified version, it supports all its previous versions including Axis1, SOAP 1.1, SOAP 1.2 and REST (REpresentational State Transfer) - the lighter version of SOAP. Axis2 integrates support from REST by just removing the SOAP headers both on the client and on the server. REST is an XMLbased protocol for invoking Web services over HTTP. Axis2 has also support for Spring Framework. The new architecture of Axis2 was introduced in first Axis2 summit held in 2004 in Colombo, Sri Lanka. It is built on Apache AXIOM, a new high performant, pull-based XML object model. The modified version is more efficient, more modular and more XML-oriented than its previous versions. It was carefully designed to support the easy addition of plug-in modules that extends its functionality and features like security and reliability. In its included features, Axis2 is quite capable of functioning as a standalone server application as well as by adding Web services interfaces to Web applications. Due to its enhanced functionality, flexibility, scalability, it has become widely accepted as a framework for practical Web Service development, which is suitable not only for commercial application development, but also as a teaching tool and as a research platform. However, Apache Axis2 is widely accepted but some of its modules are still under development: WS-ReliableMessaging - Supported by Apache Sandesha2 WS-Coordination and WS-AtomicTransaction - Supported by Apache Kandula2 WS-Security - Supported by Apache Rampart WS-Addressing -Module included as part of Axis2 core

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.

Apache Axis2 Tomcat: Installing Apache Axis2 on Tomcat


Apache Axis2 on Tomcat Tomcat is one of the most used Servlet container. It is open source and free. Tomcat can be used to develop and deploy commercial as well as free applications. In this section we will learn how to install Apache Axis2 Web service engine on the Tomcat server. After completing the tutorial you will be able to run your own Web services on the Tomcat server. Installing the Apache Axis2 engine is very simple process and you can learn in half an hour. Software Required Apache Tomcat 6 or above Apache Axis2 web achieve (.war file) JDK 6 or latest version. I am assuming that JDK is installed and configured on your computer Eclipse IDE. Eclipse is not mandatory, you can develop Web services manually

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.

Downloading and Installing Apache Axis2


Downloading and installing Apache Axis2 In this section we will download and install the Apache Axis 2 engine for testing the application. The official website of the Apache Axis2 is http://ws.apache.org/axis2/. To download the latest version of the Axis2 visit its official website at http://ws.apache.org/axis2/. You will find the latest information on the Axis2 and the link to download it. Visit the link http://ws.apache.org/axis2/ to download the current version of Apache Axis2.

<="" p="" style="border: 0px;"> Click on the download link.

<="" p="" style="border: 0px;">

Click on the 1.5.1 link.

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.

Apache Axis2 Hello World Example


Apache Axis2 Hello World Example
In this section we will develop a simple Hello World Web service and then deploy on the Axis2 engine. In the last section we have deployed the Axis2 engine on the Tomcat server. We will use the same Axis2 engine and then deploy and test the application.

About the Hello World Web service


Our Web service example is very simple example that will explain you the process of development and deployment of Web services on the Axis2 engine. The Hello World Web service will just return the "Hello World" message to the Web service client.

Directory Structure of the application


Create the following directory structure in your hard disk. You can also download the source code of the example from here if you don't want to do it manually. The source code download contains all files in proper directories.

Developing Web service


The development of Web services is easy process. You can start from the WSDL file( Contract

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)

Writing the Service Class


Our services class is "HelloWorldService.java", let's create the same. Create a java file HelloWorldService.java into \HelloWorld\webservice\net\roseindia directory and add the code shown below. Here is the code of Hello World Webservice. package net.roseindia; public class HelloWorldService { public String sayHello(String name) { System.out.println("Hello World Service called"); return "Hello : " + name; } }

Web Services Descriptor


In Axis2 the service configuration file used is

services.xml. The services.xml file must present in

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.

Compiling and building the service archieve


Go to the E:\Axis2Tutorial\Examples\HelloWorld\webservice directory and compile the java class using following command:

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 * Here is the details:


E:\Axis2Tutorial\Examples\HelloWorld\webservice>javac net/roseindia/*.java

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%)

adding: net/roseindia/HelloWorldService.java(in = 167) (out= 126)(deflated 24%)

E:\Axis2Tutorial\Examples\HelloWorld\webservice>

Deploying the Web Services


Now copy the HelloWorldService.aar into webapps/axis2/WEB-INF/services directory and restart the Tomcat. The Apache Axis2 engine will deploy the service on the server. Now open the browser and browser the url http://localhost:8080/axis2/services/listServices. Your browser should display the service name as shown below:

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.

Axis2 client - Axis2 Client example


Apache Axis2 Client code
In this section we will develop client code example to access the Hello World Web service developed in the last section. In the last section we developed and deployed the Hello World Web service. In this section we will write the Web service client code and call the web service.

Setting up the environment


In our first section of downloading and installing Axis 2 engine, we instructed you to download the binary version of Apache Axis2. Now extract the binary version(axis2-1.5.1-bin.zip) using any zip tool. After extracting the file into E:\Axis2Tutorial (assuming your tutorial directory is E:\Axis2Tutorial), you will get a directory "E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1" which contains the binary version of the Apache Axis2 engine. Now set the following environment variables: a) AXIS2_HOME=E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1 b) Add E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1\bin into path c) Add E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1\lib\* into CLASS_PATH After making the above changes the wsdl2java.bat is available for generating the client code calling Web service.

Generating the client code using wsdl2java.bat tool


Now create a new directory E:\Axis2Tutorial\Examples\HelloWorld\client and then open dos prompt and go to same directory. Now generate the client code using following command:

WSDL2Java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -o client


Here is the output of above command.

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.

cd client/src Developing the code to call the service


Create the file Test.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory. The code of Test.java file is as below: package net.roseindia; import net.roseindia.*; import net.roseindia.HelloWorldServiceStub.SayHello; public class Test { public static void main(String[] args) throws Exception { HelloWorldServiceStub stub = new HelloWorldServiceStub(); //Create the request net.roseindia.HelloWorldServiceStub.SayHello request = new net.roseindia.HelloWorldServiceStub.SayHello(); request.setArgs0("Deepak Kumar"); //Invoke the service net.roseindia.HelloWorldServiceStub.SayHelloResponse response = stub.sayHello(request); System.out.println("Response : " + response.get_return()); } }

Compiling and testing the Web service

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.

Response : Hello : Deepak Kumar


E:\Axis2Tutorial\Examples\HelloWorld\client\client\src> The client appliction makes a call to Web service and in response Web services returns the Hello

Deepak Kumar message


You have successfully tested your Web Service. Download code

In this last section we generated the client code manually and developed the client to test the Hello World Web service.

Axis2 ant wsdl2java - Learn WSDL2java utility of Axis2 with example


Axis2 ant wsdl2java Example In this last section we generated the client code manually and developed the client to test the Hello World Web service. In this section we will learn how to use wsdl2java tool from the ant build script. If you are working on the project then it's better to use the Ant wsdl2java plugin to quickly generate the client code. While developing the project the WSDL file may change several times due to change/update in the business logic, so you need a mechanism to quickly generate the latest stubs for your project. Ant provides the task to help the developer. Here you will learn to use wsdl2java tool from the Ant. Setting up Ant tool You can download the latest version of ant and install on your computer. In this section we will ne needing the ant tool to work with. If you don't know about ant please read our ant tutorial athttp://www.roseindia.net/jboss/10_minutes_guide_to_ant.shtml . Writing the build.xml file Now we will see how we can develop the code for ant build. I am assuming that you have installed Axis2 binary at E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1 and the url of WSDL file ishttp://localhost:8080/axis2/services/HelloWorldService?wsdl . You can update the build.xml file to point to the axis2 location directory in your computer. Here is the code of build.xml file. <?xml version="1.0" ?> - <project name="antwsdl2java" default="gen" basedir="."> - <path id="axis2.classpath">

- <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

You might also like