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.aar
+ SimpleService.class
+ META-INF
   + 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

Advertisements

Installing axis2 on Ubuntu host

Hey ppl, it is time for one of my super helpful mini tutorials 🙂

Its been a while since last post. I had lot of work and things on my mind. Today I decided to write mini tutorial about installing axis2, as I setup it again on my virtual machine.

Before we start, I’d like to tell you why I’m installing everythin again! 😦 It is because my old virtual machine was broken few days ago.

I had WinXP on it, and I was doing some IDEF0 and DFD diagrams. I needed ERWIN for some ER diagrams and I tried to install it. It started and took some time, and then VBox started going crazy! It shutdown WinXP virtual machine and threw me an error about it. If I just had a clue not to edit Virtual Box.xml file, I’d be very happy man right now. Anyway, I had removed some lines, or something like that and all my virtual machines and their snapshots were inaccessible. I have lost few hours trying to make it work again, but with no success. So, I’m giving you advice, FIRST READ THEN EDIT CONFIG FILES 🙂

Ok, now we will start with installing axis2.

First, you have to install java6, tomcat6 packages with next command:

sudo apt-get install sun-java6-jdk tomcat6-admin tomcat6-docs tomcat6-examples

Now, you can try accessing localhost:8080/ in your browser. If you try to get on admin pages, to deploy some servlet, you’ll get prompt for username and password. Because tomcat6 default install hasn’t those parameters in config file, you’ll have to edit /var/lib/tomcat6/conf/tomcat-users.xml. In it you’ll have to add next lines:

<role rolename=”admin”/>
<role rolename=”manager”/>
<user username=”admin” password=”password” roles=”admin,manager”/>

between tomcat-users tags. Change “password” with your own.

After this, you’ll have to restart tomcat6 with:

sudo /etc/init.d/tomcat6 restart

Then, you have to access manage-webapp link on localhost:8080/ and enter your username and password from tomcat-users.xml file.

Inside, you can do lot of things, but we will do war deploy :D. Since we are installing axis2, you must download WAR archive from this link: http://ws.apache.org/axis2/download/1_5_1/download.cgi

Then you choose path to axis2.war file, and deploy it. To access it, visit localhost:8080/axis2/. I’m pretty sure you’ll get error. It took me som time to figure out what is blocking it. The problem is in tomcat6 script in /etc/init.d directory. You have to switch tomcat security flag from yes to no. Then restart tomcat6, and you’ll have axis2 working.

That is all. Feel free to comment and ask, and I’ll feel equally free to answer them 🙂 BYE!