Creating Java web service using Axis 2 and Tomcat

Posted: June 20, 2014 in Java

Web Service:
A Web Service is a code that is exposed over the network and can be invoked using HTTP.

Why Web Service:
Using web service your functionality can be exposed to rest of the world. Web service uses SOAP to transport data. It is a open protocol. With web service the functionality can be consumed from different platforms. For example you can create a web service in your windows computer and that can be consumed from your android mobile.

Using web service you can create a loosely coupled client server environment. Which means, when the server code is changed it is not always necessary to change the client code.

Axis 2
Axis 2 is a web service/SOAP/WSDL engine provided by Apache. It is a java based implementation. Axis 2 provides complete object model and modular architecture. Using Axis 2 you can easily create a web service from a plain java class, send SOAP messages, receive SOAP message.

Creating a web service from a plain java class in eclipse:

Step 1:
Before creating a project you need to add axis 2 dependencies to your eclipse.
To do so, download axis 2 from this website http://axis.apache.org/axis2/java/core/ .
Open your eclipse and go to Window>Preferences

Expand Web Services option and click Axis2 Preferences.
Give your axis2 location in the Axis2 runtime location box:

Click OK.

Step 2:
You have added Axis2 runtime to you eclipse. Now you can create your web service.
Go to File>New>Dynamic web project

Give any project name and select Dynamic web module version 2.5. Dynamic web module 3 is not compatible with Axis 2.

Now click finish.

Step 3:
Goto project properties and select Axis 2 facet.

Click OK. Now Axis 2 libraries will be imported to your project.

Step 4:
Now expand your project window>right click on src>New>Package

 

Give any name to the package;

Now click finish.

Step 5:
Create a new java class inside the newly created package:

Step 6:
Create a simple method which returns a String
/**
 * 
 */
package sample;

/**
 * @author Vienna
 *
 */
public class WebService {
 public String getName(){
  return "Test";
 }
}
And save the class.
Step 7:
Now right click on the java class>Web Services>Create new web service

Step 8:
Make sure that correct class name is selected in the Service implementation box.
In the configuration>Web service runtime> Make sure Axis 2 is selected. If it is different then click the name and a new window will be opened.

In the new window select Axis2

Now click OK and finish the setup.

Step 9:
Publish your project in Tomcat.

Step 10:
Run your project on tomcat.
To run the project Right click on the project name>Run as>Run on server>Select Tomcat>Click finish.
Now Axis home will be opened.

Now click “Services”
Under Web Service, your project url and the method you have created in the java class will be displayed. And the Service status should be active.

Now click Web Service, wsdl file will be displayed. Copy the URL of the WSDL file

Step 11:
Click web services explorer at the top of tool bar.

If this is not available in the tool bar then,
Right click on the tool bar and select customize perspective

Now make sure that the “Launch Web Services Explorer” check box is checked and click OK

After adding this “Web Services Explorer”, Open it.
Click WSDL page icon at the right corner of the wen services explorer window

Step 12:
Paste the WSDL url and click GO.
Your web service will be added to UDDI and it will be listed under UDDI registry.

Step 13:
Expand your project  name and click WebServiceSoap11Binding
The method name will be displayed at the right side

Step 14:
Click the method name displayed at the right side.

Now click “GO”

Step 15:
Now you should be seeing the String value returned by you method in the status window.

Step 16:
You have created your web service. You can use it in your other applications.

Leave a comment