Creating JavaWebService and JavaClient using Apache Axis2

Hi all, I have got lots of requests to demonstrate creation of webservices using java and Apache Axis2 api so here i go.

1. Setup the Development Environment

1.1. First you need to set up the development environment. Following things are needed if you want to create Web Services using Axis2 and Eclipse IDE(Juno 4.2).

Some Eclipse versions have compatibility issues with Axis2. This tutorial is tested with Apache Axis2 1.5.2, Eclipse Helios and Apache Tomcat 6.

1) Apache Axis2 Binary Distribution – Download
2) Apache Axis2 WAR Distribution – Download
3) Apache Tomcat – Download
4) Eclipse IDE(Juno 4.2) – Download
5) Java installed in your Computer – Download

Then click on the Web Service –-> Axis2 Preferences and browse the top level directory of Apache Axis2 Binary Distribution.
2. Creating the Web Service Using Bottom-Up Approach

2.1 First create a new Dynamic Web Project (File –> New –-> Other…) and choose Web –> Dynamic Web Project.
2.2 Set Apache Tomcat as the Target Runtime and click Modify to install Axis2 Web Services project facet.
2.3 Select Axis2 Web Services
2.4 Click OK and then Next. There you can choose folders and click Finish when you are done

3. Create Web Service Class

/**
 *
 */
package com.spark.webservice;

/**
 * @author Mantha Pavan Kumar
 *
 */
public class JavaWebService {

	/**
	 * @param firstnumber
	 * @param secondNumber
	 * @return
	 */
	public int addNumbers(int firstnumber,int secondNumber){

		return firstnumber+secondNumber;
	}
}

Then, select File –> New –-> Other and choose Web Service.
Click Next and make sure Generate a default service.xml file is selected.
You can go to http://localhost:8080/JavaWebService/services/listServices to see your running service which is deployed by Axis2. You can see the WSDL by clicking the link JavaWebService.

Creating a Web service client
Select File –> New –> Other… and choose Web Service Client
Set he newly created Axis2 Web service (http://localhost:8080/JavaWebService/services/listServices) as the Service definition. Then configure the Server runtime as previously and click finish.

This will generate two new classes called JavaWebServiceStub.java and JavaWebServiceCallbackHandler.java. Now we can create test class for client and use our web service. Create new class called ServiceTest.java and paste following code.

/**
 *
 */
package com.spark.webservice;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import com.spark.webservice.JavaWebServiceStub.AddNumbers;
import com.spark.webservice.JavaWebServiceStub.AddNumbersResponse;

/**
 * @author Mantha Pavan Kumar
 *
 */
public class ServiceTest {

	/**
	 * @param args
	 * @throws RemoteException
	 */
	public static void main(String[] args) throws RemoteException {
		// TODO Auto-generated method stub

		JavaWebServiceStub serviceStub = new JavaWebServiceStub();
		AddNumbers addNumbers = new AddNumbers();
		addNumbers.setFirstnumber(10);
		addNumbers.setSecondNumber(20);

		AddNumbersResponse addNumbersResponse = serviceStub.addNumbers(addNumbers);
		System.out.println( addNumbersResponse.get_return());

	}

}

Now you can run the above code as java application and you will get the output
follow these screen shots for creation of webservices Creation of Java WebService
Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s