Friday, December 26, 2014

How to convert a simple Java class to a Webservice with Axis2 [Deploying POJOs]

It has never been this easy to convert your class into a Webservice in few steps with Apache Axis2.
Apache Axis2 is a SOAP processing engine and it is hand in hand with WSO2 SOA middleware platform.

Prerequsists:

JDK 1.7/1.6


How to create Axis2 service
1. Create a new Axis2 service project in Eclipse

2. Tick 'Create new Axis2 Service ' and click Next
3. Provide Project Name, package name and Class name and click Finish.




As you can see the project structure will contain the services.xml file which contains the details of the exposed service.

4. Now you can add methods to the Class as follows:

package com.world.hello;

public class Hello{

   
    public String greetInSinhala(String name){
        return "Ayubowan "+ name;
    }
   
    public String greetInEnglish(String name){
        return "Hello "+ name;
    }
}


4. Now you can add methods to the Class as follows:
5. Now create the service archive file (*.aar extension) by right click on the project and select 'Export Project as Deployable Archive'.

 How to deploy *.aar file in Axis2 enabled Tomcat 
 1. Copy the axis2 war file to apache-tomcat-7.0.47/webapps directory and start the server.
You will see the axis2 directory under Tomcat webapps directory once the server is started.
2.Copy the HelloWorld.aar file to apache-tomcat-7.0.47/webapps/axis2/WEB-INF/services directory
3. Access below url and you should be able to see the deployed service and the available methods.

http://localhost:8080/axis2/services/listServices



That is the end of the story :)





No comments: