Saturday, January 7, 2012

Develop Code-First Web Services using Axis2


Overview:
This article is simple, but provides a good foundation of creating a Web Service from an existing Java code using Apache Axi2 and Maven3.

Pre-requisites:
Apache Axis2 1.6.x
Apache Tomcat
Apache Maven3

Sample code used for this can be found here.
Steps:
1. Create a Maven project,

mvn archetype:generate -DgroupId=org.fazlan -DartifactId=org.fazlan.employee.service -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false

Now, the project structure should be like the following,

org.fazlan.employee.service
`-- src
     |-- main
     |    `-- java
     |         `-- org
     |             `-- fazlan
     |                 `-- employee
     |                     `-- service
     |     
     `-- test 
          `-- java
               `-- org
                   `-- fazlan

2. Update the pom.xml to add the 'axis2-aar-maven-plugin' plugin.
 <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/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>org.fazlan</groupId>  
  <artifactId>org.fazlan.employee.service</artifactId>  
  <packaging>aar</packaging>  
  <version>1.0-SNAPSHOT</version>  
  <name>org.fazlan.employee.service</name>  
  <url>http://maven.apache.org</url>  
  <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.axis2</groupId>  
         <artifactId>axis2-aar-maven-plugin</artifactId>  
         <version>1.6.1</version>  
         <!--<version>${orbit.version.axis2}</version>-->  
         <extensions>true</extensions>  
         <configuration>  
           <aarName>org.fazlan.employee.service</aarName>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  


3. I have a service class to manage employee details, and the classes looks like this,
 POJO to model employee,  
 //org.fazlan.employee.service.beans.Employee.java  
 public class Employee {  
   private int id;  
   private String firstName;  
   private String lastName;  
   // getter and setter methods  
 }  
 POJO to model the employee exception,  
 //org.fazlan.employee.service.exceptions.EmployeeException.java  
 public class EmployeeException extends java.lang.Exception {  
 }  
 The service class with the logic to manage employees,  
 //org.fazlan.employee.service.EmployeeService.java  
 public class EmployeeService {  
   private static final Map employeeMap = new HashMap();  
   private static int index = 0;  
   public int addEmployee(Employee employee) throws EmployeeException {  
    employee.setId(++index);  
    employeeMap.put(index, employee);  
    return index;  
   }  
   public Employee getEmployee(int employeeId) throws EmployeeException {  
    return employeeMap.get(employeeId);  
   }  
   public Employee[] getAllEmployees() throws EmployeeException{  
    return employeeMap.values().toArray(new Employee[employeeMap.size()]);  
   }  
 }  


4. Create the service descriptor src/main/resources/META-INF/services.xml as follows,
 <service name="EmployeeService" scope="application">  
 <messageReceivers>  
    <!--messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"  
         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/-->  
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>  
  </messageReceivers>  
   <parameter name="ServiceClass">  
     org.fazlan.employee.service.EmployeeService  
   </parameter>  
 </service>  

5. Now the project structure should look like the following,
org.fazlan.employee.service
`-- src
|-- main
|   |-- java
|   |    `-- org
|   |         `-- fazlan
|   |              `-- employee
|   |                   `-- service
|   |                       |-- beans
|   |                       |-- exceptions
|   |                       `-- util
|   `-- resources
|       `-- META-INF
`-- test
     `-- java
         `-- org
             `-- fazlan

6. Build the project and pack the aar file.

mvn clean install

7. Deploy the axis2.war from the Axis2 distribution to Tomcat. After deploying you can verify by accessing http://localhost:8080/axis2/

8. Now, deploy the org.fazlan.employee.service.aar via the Axis2 administrator's console. (default username/password is admin/axis2).

9. After successfully deploying the service, you can access the WSDL via http://localhost:8080/axis2/services/EmployeeService?wsdl


Summary:

This article looked at how you can create a Web Service using Axis2 in a nutshell.

No comments:

Post a Comment