Home Webservices Axis2 Apache Axis2 Hello World Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Apache Axis2 Hello World Example

Advertisement
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 the META-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 is org.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" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 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  

   

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2009

Related Tutorials

Ask Questions?    Discuss: Apache Axis2 Hello World Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rich Yim
June 10, 2011
Some typo

From my test, I found the http://localhost:8080/axis2/services/HelloWorldService?wsdl does not exist. It should be http://localhost:8080/axis2/services/HelloWorld?wsdl. After changing it back to correct path, everything works fine. By the way, thanks for your sharing. I got confused before. After reading your page, I am okay with AXIS2 now and I love it. Rich
joaomartins
December 9, 2013
passing parameters in url

Hi, Can you pls explain how you pass parameters in your example? All the best
DMCA.com