This sample demonstrates how to write a web service to be deployed in Axis2 SOAP engine using AXIOM in 9 simple steps. I have used Eclipse as the IDE for this sample.
First let's look at how to write the web service and then we'll see how to deploy it in Axis2. And at last how to test the service using SoapUI.
To follow this sample you need Axis2 and Eclipse downloaded in your computer. The directory in which you have installed axis will be refered to as <AXIS2_HOME> bellow. For this example I'm using Axis2 version 1.6.2.
This sample service is a order processing service which has only three basic operations
- AddOrder - Allows users to add orders(Only argument)
- GetPrice - Allow users to get the price of a specified symbol(Argument and return value)
- GetOrders - Allow users to retrieve back all placed orders(Only Return value)
Writing the Service
1. Create a new "Java Project" and lets name it as "SampleOrderProcessingService".
2. Then add,
as external JARs to the project. These JARs can be found in <AXIS2_HOME>/lib
3. After exporting the JARs you add the following code listing in a class with what ever the name you like, lets name it as "SampleOrderProcessingService.java".
AddOrder method represents AddOrder operation in the web service and it takes order as an argument and returns nothing. If you take a look at the method body you will see that the method is expecting it's request to have child elements called "symbol" and "price". Therefor the clients must meet this expectation when sending requests, otherwise the service will fail. So the request must look like something similar to bellow(not exactly),
Once the order is received it will fetch the symbol and price and store it in its cache.
It must be noted that clients of axiom services must be aware of the expected request format by the service and adhere to that format when sending requests.
The GetPrice method takes symbol as an argument and it returns the price as its return value. The returned XML structure is created using AXIOM in the method body and it will look like as follows,
GetOrders method takes an dummy argument. This is due to the fact that Axis2 does not have a message handler which handles operations that has only return values. It creates a XML tree which contains all the orders and returns it.
Writing the services.xml
After finishing writing the service you have to specify the operations and other information in a file called services.xml for axis2 to be able to deploy it.
4. Create a folder called META-INF in your project at the root level.
5. Then add a file called services.xml into it.
6. Add the following contents to the services.xml
The web service is named as "SampleOrderProcessingService" and is given a description as "Sample Order Processing Serviece"(Line 2-5).
For each operation in the service you must include a <operation> element. The "name" attribute must match the method name in the class we wrote in step 3. So there are there operation elements for each method in the class. And also it's possible to have methods that are not exposed as operations in the java class.
Another important thing is the messageReceiver specified in the operation. This message receiver must match the method signature of the method corresponding to the operation.
Operation | Method Signature | Message Handler
AddOrder | Only arguments, no return value | RawXMLINOnlyMessageReceiver
GetPrice | Takes an argument, returns a value | RawXMLINOutMessageReceiver
GetOrders | Takes a dummy argument, returns a value | RawXMLINOutMessageReceiver
GetOrders Method had to use a INOut messages receiver and take a dummy argument, though it does not require to take an argument. This is because Axis2 does not have a OUTOnly message receiver
At Last, you have to specify the full qualified name of the service class(Line 15)
Deploying the service
Now all you have to do is pack .class file and services.xml into a .aar and deploy it in Axis2 server.
7. Right click on the project and Export it as a JAR (SampleOrderProcessingService.jar) to a desired location
8. Rename the .jar file to have extension .aar. Change SampleOrderProcessingService.jar to SampleOrderProcessingService.aar
This is because .aar is the archive type recognized by Axis.
9. Copy SampleOrderProcessingService.aar file into <AXIS2_HOME>/repository/services and Axis2 will automatically deploy your service(You don't have to restart Axis2 server). If the service is properly deployed you will see something like follows in your terminal,
Also you must be able to see SampleOrderProcessingService listed if you goto http://localhost:8080/axis2/services/
Testing the service
Once the service is deployed, we can test it to verify it's functionality. For this purpose lets use SoapUI.
Create a SoapUI project for the WSDL http://localhost:8080/axis2/services/SampleOrderProcessingService?wsdl.
Before sending any request make sure that xmlns:ser is set to the namespace given in the server class. In this case xmlns:ser="http://orderprocessingservice.com" since we have defined namespace to be "http://orderprocessingservice.com" in line:21 of the service class.
Following is a valid AddOrder message. Note that we have to add <symbol> and
<price> elements to the request.
When you send this message, you must be able to see a message similar to below in the terminal which runs the Axis2 server
Likewise you can send requests to other operations, bellow screenshot shows the requests along with their responses for GetOrders and GetPrice operations.
That's all from this post. Hope it helped you. Also I'm expecting to do a post on how to write an AXIOM client to consume this service. Please leave your comments and feedback.