Axis2 Code First Approach Web Services in Java

By Arvind Rai, January 18, 2014
In this page we will learn code first approach to develop axis2 web service. We write code of web service interface and generate WSDL file that is given to the client. The service is deployed in the axis2 application. For that first we need to deploy axis2 in our tomcat and then access the axis2 and deploy our web service. For full demo we have taken a simple calculator service example. We will develop server and client for axis2 code first approach web service.

Create Server for Axis2 Code First Approach Web Services

To create server for axis2 code first approach, we will go step by step. We write code that will generate WSDL response. In our example we have taken simple calculator web service which wills serve the multiplication of two numbers. Client will send the request with two numbers for multiplication and server will respond the multiplied number.

Software Requirement

To develop the server, we must be ready with software given below.
1. JDK 5 or above
2. Tomcat
3. Eclipse
4. Axis2 War File
Download latest version of axis2 war file from the location and install it in tomcat. After installation of axis2, you will be able to access http://localhost:8080/axis2/ URL.

Develop Code for Server

Find the files which are required to develop our calculator web service.
package com.concretepage;
import javax.jws.WebService;
public interface Calculator {
	public Integer multiply(Integer num1, Integer num2);
We have an interface which is annotated with @WebService.
package com.concretepage;
import javax.jws.WebService;
@WebService(serviceName = "SimpleCalculatorService",
portName = "cport",
endpointInterface = "com.concretepage.Calculator")
public class SimpleCalculator implements Calculator {
	public Integer multiply(Integer num1, Integer num2) {
		Integer result = num1 * num2;
		return result;
This class is the implementation of our interface. We need to declare service name, port name and end point interface as the attribute of @WebService annotation.
      This Web Service multiply two numbers.
   <parameter name="ServiceClass">com.concretepage.SimpleCalculator</parameter>
   <operation name="multiply">
In the class path there must be a file as META-INF/services.xml which will contain service class name. In services.xml, we need to provide the fully qualified path of calculator service implementation class.
Axis2 Code First Approach Web Services in Java

Deploy Service in Axis2

To deploy our service, we will create .aar file. To create .aar file, open the command prompt and we need to go where our classes resides. In our case I will go to bin directory of project root path and will run the command as given below.

jar cvf SimpleCalculatorService.aar *
Find the image.
Axis2 Code First Approach Web Services in Java
Upload SimpleCalculatorService .aar file in axis2. To upload file open the URL as http://localhost:8080/axis2/ and go to
1. Administration
2. Login with admin/axis2
3. Go to upload services and upload the SimpleCalculatorService .aar file. It will be deployed in \webapps\axis2\WEB-INF\services location.
Axis2 Code First Approach Web Services in Java
To check whether our service is successfully installed, run the URL

Create Client for Axis2 Code First Approach Web Services

To consume the web service, we need to create the client stubs. The URL http://localhost:8080/axis2/services/SimpleCalculatorService?wsdl will help us to create the stub. Save the output of this URL as calcwsdl.wsdl and put it in the class path in the client project. Find the maven dependency.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
To generate the source code of SimpleCalculator stub, run the pom.xml with the command clean generate-sources . We will get two classes as


Now create Client as below
package com.concretepage.client;
import java.rmi.RemoteException;
import com.concretepage.SimpleCalculatorServiceStub;
import com.concretepage.SimpleCalculatorServiceStub.Multiply;
import com.concretepage.SimpleCalculatorServiceStub.MultiplyResponse;
public class Client {  
    public static void main(String[] args) throws RemoteException {
    	SimpleCalculatorServiceStub stub = new SimpleCalculatorServiceStub();
    	Multiply request = new Multiply();
        MultiplyResponse  response = stub.multiply(request);
The eclipse configuration for client will look like
Axis2 Code First Approach Web Services in Java
Run and see the output. In this way we have consumed the web service.

Download Source Code


©2022 | Privacy Policy | Contact Us