Thursday, November 27, 2014

How to Create an Axis2 Web Service

In this post I'm going to explain a simple way to implement an Axis2 Web Service and how to deploy it.

This is a simple Order Processing service. I will start with a simple Java class and show you how to make a web service from it by including necessary configuration and packaging.

First create a maven project with this pom.
 

    4.0.0
    OrderProcessingService
    OrderProcessingService
    1.0    


Next add a Java class to your project with the name of the web service. This Java class contains all the methods which will be exposed as the operations of the web service.
import java.util.HashMap;  
  
/** 
 * Created with IntelliJ IDEA. 
 * User: manorama 
 * Date: 11/27/14 
 * Time: 2:22 PM 
 * To change this template use File | Settings | File Templates. 
 */  
public class OrderProcessingService {  
  
    private HashMap ordersMap = new HashMap();  
  
    public double getAmount(String orderID) {  
  
        Double price = (Double) ordersMap.get(orderID);  
        if(price != null){  
            return price.doubleValue();  
        }  
        return 0.00;  
    }  
  
    public void submitOrder(String orderID, double amount) {  
  
        ordersMap.put(orderID, amount);  
    }  
}

Next we have to add a service descriptor to the project. This is an xml file with the name services.xml. Each Axis2 service must have a service descriptor. The service descriptor informs Axis2 about the service.


Create a folder META-INF inside resources folder. And create an xml file services.xml inside that folder.

For the Order Processing Service, the service descriptor is as follows.

    
        Order Processing Service
    
    
        
        
    
    
        OrderProcessingService
    

We write the details about a web service inside the <service > element. We define message receivers that need to be used for the web service operations inside < messageReceivers > element. < parameter > element contains the name of the service implementation class.

Now we're done with the coding. Next step is to package the service implementation and configuration files according to the Axis2 format.

First compile the above code. It will create the directory target which contains the compiled class files inside the classes folder in it. Then simply change the directory to target/classes.

$cd target/classes

You can see the .class files and the META-INF directory containing the services.xml.

Now run the following command to package all the files into an aar archive with the relevant service name. In this example it is OrderProcessingService.aar

$jar -cvf OrderProcessingService.aar  *

To deploy the web service, copy the generated OrderProcessingService.aar into $AXIS2_HOME/repository/services

You can see the deployed service once you start the Axis2 server and point the browser to http://localhost:8080/axis2/services.

To invoke this web service using a client follow this tutorial.

Reference :
[1] http://wso2.com/library/95/

No comments: