Sunday, January 8, 2012

Developing a Web Service Client using Axis2

Overview:
This article looks at how to create a service stub using Axis2 from a WSDL also know as a Web Service contract(refer here).

Pre-requisite:
Apache Axis2 1.6.1
Apache Tomcat
Apache Maven3

Sample code used can be found here.
Steps:
1. Create a project using the following,
mvn archetype:generate -DgroupId=org.fazlan -DartifactId=org.fazlan.employee.service.stub -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false

2. Now, access the WSDL file (http://localhost:8080/axis2/services/EmployeeService?wsdl) and save as org.fazlan.employee.service.stub/src/main/resources/EmployeeService.wsdl.

3. Update the pom.xml to include the axis2 dependencies and plug-ins to generate the artifacts from the WSDL file as follows,

1: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3: <modelVersion>4.0.0</modelVersion>
4: <groupId>org.fazlan</groupId>
5: <artifactId>org.fazlan.employee.service.stub</artifactId>
6: <packaging>jar</packaging>
7: <version>1.0-SNAPSHOT</version>
8: <name>org.fazlan.employee.service.stub</name>
9: <url>http://maven.apache.org</url>
10: <dependencies>
11: <dependency>
12: <groupId>org.apache.axis2</groupId>
13: <artifactId>axis2</artifactId>
14: <version>${axis2.version}</version>
15: </dependency>
16: <dependency>
17: <groupId>wsdl4j</groupId>
18: <artifactId>wsdl4j</artifactId>
19: <version>${wsdl4j.version}</version>
20: </dependency>
21: <dependency>
22: <groupId>org.apache.ws.commons.axiom</groupId>
23: <artifactId>axiom-api</artifactId>
24: <version>${axiom.api.version}</version>
25: </dependency>
26: <dependency>
27: <groupId>commons-logging</groupId>
28: <artifactId>commons-logging</artifactId>
29: <version>${commons-logging.version}</version>
30: </dependency>
31: <dependency>
32: <groupId>org.apache.neethi</groupId>
33: <artifactId>neethi</artifactId>
34: <version>${neethi.version}</version>
35: </dependency>
36: <dependency>
37: <groupId>org.apache.ws.commons.schema</groupId>
38: <artifactId>XmlSchema</artifactId>
39: <version>${XmlSchema.version}</version>
40: </dependency>
41: <dependency>
42: <groupId>org.apache.ws.commons.axiom</groupId>
43: <artifactId>axiom-impl</artifactId>
44: <version>${axiom-impl.version}</version>
45: </dependency>
46: <dependency>
47: <groupId>org.apache.axis2</groupId>
48: <artifactId>axis2-transport-local</artifactId>
49: <version>${axis2.version}</version>
50: </dependency>
51: <dependency>
52: <groupId>org.apache.axis2</groupId>
53: <artifactId>axis2-transport-http</artifactId>
54: <version>${axis2.version}</version>
55: </dependency>
56: </dependencies>
57: <build>
58: <plugins>
59: <plugin>
60: <groupId>org.apache.maven.plugins</groupId>
61: <artifactId>maven-antrun-plugin</artifactId>
62: <version>1.1</version>
63: <executions>
64: <execution>
65: <id>source-code-generation</id>
66: <phase>process-resources</phase>
67: <goals>
68: <goal>run</goal>
69: </goals>
70: <configuration>
71: <tasks>
72: <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true">
73: <arg line="-uri src/main/resources/EmployeeService.wsdl -u -uw -o target/generated-code
74: -p org.fazlan.employee.service.stub
75: -ns2p http://service.employee.fazlan.org=org.fazlan.employee.service.stub,http://beans.service.employee.fazlan.org/xsd=org.fazlan.employee.service.stub.beans.xsd,http://exceptions.service.employee.fazlan.org/xsd=org.fazlan.employee.service.stub.exceptions.xsd"/>
76: <classpath refid="maven.dependency.classpath"/>
77: <classpath refid="maven.compile.classpath"/>
78: <classpath refid="maven.runtime.classpath"/>
79: </java>
80: </tasks>
81: </configuration>
82: </execution>
83: </executions>
84: </plugin>
85: <plugin>
86: <groupId>org.codehaus.mojo</groupId>
87: <artifactId>build-helper-maven-plugin</artifactId>
88: <executions>
89: <execution>
90: <id>add-source</id>
91: <phase>generate-sources</phase>
92: <goals>
93: <goal>add-source</goal>
94: </goals>
95: <configuration>
96: <sources>
97: <source>
98: target/generated-code/src
99: </source>
100: </sources>
101: </configuration>
102: </execution>
103: </executions>
104: </plugin>
105: </plugins>
106: </build>
107: <properties>
108: <axis2.version>1.6.1</axis2.version>
109: <wsdl4j.version>1.6.2</wsdl4j.version>
110: <commons-logging.version>1.1.1</commons-logging.version>
111: <neethi.version>2.0.4</neethi.version>
112: <axiom.api.version>1.2.12</axiom.api.version>
113: <axiom-impl.version>1.2.12</axiom-impl.version>
114: <XmlSchema.version>1.4.7</XmlSchema.version>
115: </properties>
116: </project>

4. Build the project to generate the artifacts required,

mvn clean install

The generated artifacts will be packaged in the org.fazlan.employee.service.stub-1.0-SNAPSHOT.jar. You then add this jar as a dependency to the client project that needs to access the Employee Web Service.

5. Write a simple Java client (You may need to include all the axis2 dependencies to run this client).

1: import org.apache.axis2.AxisFault;
2: import org.fazlan.employee.service.stub.beans.xsd.Employee;
3: public class Main {
4: public static void main(String... args) throws Exception {
5: EmployeeServiceStub service = new EmployeeServiceStub();
6: Employee e = new Employee();
7: e.setId(1);
8: e.setFirstName("Fazlan");
9: e.setLastName("Sabar");
10: int id = service.addEmployee(e);
11: System.out.println(id);
12: Employee[] employees = service.getAllEmployees();
13: for(Employee em : employees) {
14: System.out.println(em.getId());
15: System.out.println(em.getFirstName());
16: System.out.println(em.getLastName());
17: System.out.println("---------------");
18: }
19: }
20: }

6. Sample output.

0
Fazlan
Sabar
---------------
1
Fazlan
Sabar
---------------
Summary:
This articles looked at how to generate artifacts using Axis2 with Maven3 to interact with the Web Service contract (WSDL).

No comments:

Post a Comment