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:
- Develop the Service Class
- Develop the service descriptor e.g. services.xml
- 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;
|
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:
targetNamespace="http://roseindia.net">
binding="ns:HelloWorldServiceSoap11Binding">
.HelloWorldServiceHttpSoap11Endpoint/" />
.HelloWorldServiceHttpSoap12Endpoint/" />
.HelloWorldServiceHttpEndpoint/" />
0
You have successfully deployed the Hello World service on Tomcat server. In the next section we will create the client and test the application.