Home  >  Web Services

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 http://axis.apache.org/axis2/java/core/download.cgi 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.
Calculator.java
package com.concretepage;
import javax.jws.WebService;
@WebService(name="SimpleCalculator")
public interface Calculator {
	public Integer multiply(Integer num1, Integer num2);
}
 
We have an interface Calculator.java which is annotated with @WebService.
SimpleCalculator.java
package com.concretepage;
import javax.jws.WebService;
@WebService(serviceName = "SimpleCalculatorService",
portName = "cport",
endpointInterface = "com.concretepage.Calculator")
public class SimpleCalculator implements Calculator {
	@Override
	public Integer multiply(Integer num1, Integer num2) {
		Integer result = num1 * num2;
		return result;
	}
}
 
This class is the implementation of our Calculator.java interface. We need to declare service name, port name and end point interface as the attribute of @WebService annotation.
services.xml
<service>
   <description>
      This Web Service multiply two numbers.
   </description>
   <parameter name="ServiceClass">com.concretepage.SimpleCalculator</parameter>
   <operation name="multiply">
      <messageReceiver
      class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
   </operation>   
</service>
 
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
http://localhost:8080/axis2/services/SimpleCalculatorService?wsdl

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.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concretepage</groupId>
<artifactId>WBClient</artifactId>
<version>1</version>
<packaging>jar</packaging>
<dependencies>
    <dependency>
      <groupId>org.apache.axis2</groupId>
      <artifactId>axis2-kernel</artifactId>
      <version>1.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.axis2</groupId>
      <artifactId>axis2-adb</artifactId>
      <version>1.6.2</version>
    </dependency> 
    <dependency>
	  <groupId>org.apache.axis2</groupId>
	  <artifactId>axis2-transport-local</artifactId>
	  <version>1.6.2</version>
    </dependency>
    <dependency>
	  <groupId>org.apache.axis2</groupId>
	  <artifactId>axis2-transport-http</artifactId>
	  <version>1.6.2</version>
    </dependency>
</dependencies>
<build>
        <plugins> 
            <plugin>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
                <version>1.6.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsdl2code</goal>
                        </goals>
                        <configuration>
                            <packageName>com.concretepage</packageName>
                            <wsdlFile>src/main/resources/calcwsdl.wsdl</wsdlFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
 
To generate the source code of SimpleCalculator stub, run the pom.xml with the command clean generate-sources . We will get two classes as

1. SimpleCalculatorServiceCallbackHandler.java
2. SimpleCalculatorServiceStub.java

Now create Client Client.java as below
Client.java
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();
    	request.setNum1(10);
    	request.setNum2(15);
        MultiplyResponse  response = stub.multiply(request);
        System.out.println(response.get_return());
 	}
} 
  
The eclipse configuration for client will look like
Axis2 Code First Approach Web Services in Java
Run Client.java and see the output. In this way we have consumed the web service.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us