Making a simple web service using axis2

Me again! 8)

In last article related to Axis, we talked about installing Axis2 on tomcat server. Today, I’m going to write about making a basic web service, using axis2. Now, I do not have the great knowledge on using web services, but I managed to make them work. For following this mini tutorial, you are going to need Linux, Tomcat6, Java, Axis2. If you do not have that, just follow this tutorial.

First, let us decide what will our service do! Since it is going to be simple tutorial and simple service too, let us make service that will calculate sum of 2 numbers.

Service name: SimpleService
Methods: Add; arguments: a(int),b(int); returns int;

Before we get into coding, lets see what makes web service.
When you hear web service, you have to think about it being on some server. To put it on server is called deployment. So, we are going to deploy our web service, when we make it. What are we deploying? When making java web service, we are making an archive. That archive has extension aar, which is a basic jar extension, but renamed. What it has to contain? Main thing is, of course, our class, with methods. Another very important part is particular file called services.xml. Now, we are not going to get into ways of writing it, but you need to check this tutorial about it. This file has to be placed into META-INF document, so axis can find it and read it.

Folder structure:

+ SimpleService.class
   + services.xml

I hope you’ll get the idea 8)
Let us see the services.xml file:

<service name="SimpleService"> <parameter name="ServiceClass" locked="false">SimpleService </parameter> <operation name="add"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service>

Now, service name has to be the same as the name of the aar archive. In parameter tags, you have to put the whole path to the service class. In more complex projects, you’ll have packages, and you have to put all those dots, something like: folder1.folder2.Classfile. For our concern, it is only one file, so its name fill be enough. For the operation tag, you have to put the name of the method we will make, and this massageReceiver is the basic and default one.
Now, it is time for java coding. Stay calm, it is not going to be hard, just a few rows. Here it is:

public class SimpleService {
	public int add(int a, int b){
		return a+b;

It wasn’t hard, was it?

Now all you have to do is to deploy it.
You do not have to write client for it, because you can try it through browser. If you are working on localhosto server, then address should be something like this:

From here, you can see where is the name of method, and which are the arguments.

And at last, but not at least, accessing this link http://localhost:8080/axis2/services/SimpleService?wsdl, you’ll get the WSDL, which is the most important part of communication between client and web services on server, but since this tutorial is for beginners, we shall not talk about it. To be honest, I don’t know to write it, but luckily axis makes it from your arr automatically. YEY for Axis!!! 😉

I hope this will be useful for someone who is facing the web services for the first time, because I was the newb not so long ago, and I know how it is to have sooo many questions, and not adequate literature. Feel free to ask if you have some questions about whis topic, and I’ll try to answer if I can. 8) Bye