• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

Java Tutor Online

  • Home
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • Contact Us

Axis 2 web service Tutorial

In this tutorial we will create and deploy a web service using Axis 2

What is Apache Axis 2?

Apache Axis 2 is a core engine for Web services that is a successor of the Apache Axis Soap Project. It is a highly flexible, secured platform for Web Services.

Axis2 Web Service HelloWorld Example.

We will develop a simple Web service example and deploy on the Axis2 engine. To do this first Axis2 should be downloaded and installed into Tomcat. From Axis download we should extract the axis2.war file and place it in the webapps folder of Tomcat. This webservice will return a simple message to the client.

Folder Structure for the HelloWorld Web Service

A folder structure as below can be created
Axis2WebService

Development of the Axis2 Web Service

There are two approaches to develop webservices.

1) We can start with a wsdl file and develop the code from it (Contract first approach)
2) We can start with the code (Code first approach) . This way is mostly used.


We will try the second approach here for the example.

Steps to Follow

  1. Develop the java class (Service class).
  2. Develop the services.xml (service descriptor)
  3. Compile the java class and create the aar file for the webservice.

Step 1.

Creating the Web Service class

1
2
3
4
5
6
7
package pac.example;
public class HelloWorld {
public String message(String name) {
System.out.println("Hello World");
return "Hello : " + name;
}
}

Step 2.

Creating the services.xml for the Web Service

1
2
3
4
5
6
<service>
<parameter name="ServiceClass" locked="false">pac.example.HelloWorld</parameter>
<operation name="message">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>

HelloWorld is the Service

org.apache.axis2.rpc.receivers.RPCMessageReceiver is the message receiver class.
The method exposed by the webservice is message
Step 3.

Creating the aar file of the Web Service

We use javac HelloWorld.java to generate the HelloWorld.class file.
Then we go to the C:\HelloWorld\webservice directory and do jar cvf HelloWorld.aar * to get the aar file

C:\HelloWorld\webservice>jar cvf HelloWorld.aar *

1
2
3
4
5
6
7
added manifest
ignoring entry META-INF/
adding: META-INF/services.xml(in = 230) (out= 153)(deflated 33%)
adding: pac/(in = 0) (out= 0)(stored 0%)
adding: pac/example/(in = 0) (out= 0)(stored 0%)
adding: pac/example/HelloWorld.class(in = 634) (out= 378)(deflated 40%)
adding: pac/example/HelloWorld.java(in = 162) (out= 124)(deflated 23%)

Deploying the Axis2 Web Service

Place the aar file in the %CATALINA_HOME%\webapps\axis2\WEB-INF\services directory.

Where %CATALINA_HOME% is the home directory of tomcat

After starting Tomcat we visit the http://localhost:8080/axis2/services/listServices

on clicking the HelloWorld hyperlink we get the below output for the wsdl file

The url of the wsdl is http://localhost:8080/axis2/services/HelloWorld?wsdl

Output of http://localhost:8080/axis2/services/HelloWorld?wsdl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<wsdl:definitionsxmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://example.pac"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://example.pac">
<wsdl:types>
<xs:schemaattributeFormDefault="qualified"elementFormDefault="qualified" targetNamespace="http://example.pac">
<xs:element name="message">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="messageResponse">
<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="messageRequest">
<wsdl:part name="parameters" element="ns:message"/>
</wsdl:message>
<wsdl:message name="messageResponse">
<wsdl:part name="parameters" element="ns:messageResponse"/>
</wsdl:message>
<wsdl:portType name="HelloWorldPortType">
<wsdl:operation name="message">
<wsdl:input message="ns:messageRequest" wsaw:Action="urn:message"/>
<wsdl:output message="ns:messageResponse" wsaw:Action="urn:messageResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoap11Binding" type="ns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="message">
<soap:operation soapAction="urn:message" 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="HelloWorldSoap12Binding" type="ns:HelloWorldPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="message">
<soap12:operation soapAction="urn:message" 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="HelloWorldHttpBinding" type="ns:HelloWorldPortType">
<http:binding verb="POST"/>
<wsdl:operation name="message">
<http:operation location="message"/>
<wsdl:input>
<mime:content type="text/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port name="HelloWorldHttpSoap11Endpoint" binding="ns:HelloWorldSoap11Binding">
<soap:address location="http://localhost:8080/axis2/services/HelloWorld.HelloWorldHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldHttpSoap12Endpoint" binding="ns:HelloWorldSoap12Binding">
<soap12:address location="http://localhost:8080/axis2/services/HelloWorld.HelloWorldHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldHttpEndpoint" binding="ns:HelloWorldHttpBinding">
<http:address location="http://localhost:8080/axis2/services/HelloWorld.HelloWorldHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

We have created a webservice called HelloWorld and deployed it in Tomcat.

Creating the Client Code for the Webservice

Here we will write the Web service client code for the HelloWorld Service we had created and call the web service.

Setting the Path and CLASSPATH for Axis.

1) We Create a environment variable called AXIS2_HOME and point to the home directory of Axis binary download.

2) Set the Path Environment to point to the AXIS2_HOME\bin directory.
3) Set te CLASSPATH environment to point to AXIS2_HOME\lib\*

After creating these configurations the wsdl2java.bat will be available for use in the command line to generate the webservice client code.

Creating the client code for the webservice usig wsdl2java.bat

By using the wsdl2java command we generate all the client code java files.

1
2
3
4
5
6
Using AXIS2_HOME: C:\Users\dell\Downloads\axis2-1.2
 
C:\HelloWorld\client>wsdl2java -uri http://localhost:8080/axis2/services/HelloWo
 
rld?wsdl -o C:\HelloWorld\client
Using JAVA_HOME: C:\Program Files\Java\jdk1.6.0_24

Below are the java files generated in the client folder

1
2
3
4
5
6
HelloWorldHelloWorldHttpEndpointCallbackHandler.java
HelloWorldHelloWorldHttpEndpointStub.java
HelloWorldHelloWorldHttpSoap11EndpointCallbackHandler.java
HelloWorldHelloWorldHttpSoap11EndpointStub.java
HelloWorldHelloWorldHttpSoap12EndpointCallbackHandler.java
HelloWorldHelloWorldHttpSoap12EndpointStub.java

Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package pac.example;
import pac.example.HelloWorldHelloWorldHttpEndpointStub;
public class Client
{
public static void main(String[] args) throws Exception {
HelloWorldHelloWorldHttpEndpointStub stub = new HelloWorldHelloWorldHttpEndpointStub();
// Create the request
HelloWorldHelloWorldHttpEndpointStub.Message request =new HelloWorldHelloWorldHttpEndpointStub.Message();
 
request.setArgs0("Chinmay");
HelloWorldHelloWorldHttpEndpointStub.MessageResponse response=stub.message(request);
System.out.println("Response : " + response.get_return());
}
}

OutPut

1
Response : Hello : Chinmay

Filed Under: Webservice

Reader Interactions

Comments

  1. Sebastián Plaza Sánchez says

    April 12, 2014 at 10:51 am

    Great ! Really simple tutorial !
    Thank you very much !

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
[email protected]

Recent Posts

  • How to Learn Java in One day? | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
Copyright © 2025 JavaTutorOnline