Quantcast
Viewing all articles
Browse latest Browse all 9

Axis 2 client

In this tutorial we are going to code an axis 2 client based on wsdl2java approach. We are going to connect to a HighScores Service which we have created in previous tutorial – Axis2 WSDL2Java approach

In this tutorial we are going to discuss following topics:
- Setting up Maven 3 Axis2 project in Eclipse
- Generating client classes from WSDL file
- Using Unit Test for testing client

Setting up Maven 3 Axis2 project in Eclipse

Let’s begin with setting up an eclipse maven project. Follow this steps:

  • In a Project Explorer window right-click and select New -> Project...
  • Pick Maven Project and click Next
  • On the Select project name and location screen select Create a simple project ... and click Next
  • Enter Group Id, Artifact Id and Package (select JAR) values
  • Click Finish

Generating client classes from WSDL file

Ok. Now we are ready to generate WebService client code. Follow this steps:

  • Copy HighScoreService.wsdl (take it from Axis2 WSDL2Java approach) to src/main/resources/wsdl directory
  • add axis2-wsdl2code-maven-plugin to your pom.xml file
<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>
             <unpackClasses>true</unpackClasses>
             <databindingName>xmlbeans</databindingName>
             <packageName>com.itcuties.ws</packageName>
             <wsdlFile>src/main/resources/wsdl/HighScoreService.wsdl</wsdlFile>
             <syncMode>sync</syncMode>                                                            
             </configuration>
         </execution>
 </executions>
</plugin>
  • run mvn generate-sources
  • In directory target\generated-sources\axis2\wsdl2code\src you will find newly generated java classes. This is our client code. The most important generated codes are *Stub.java code and the service interface.
  • Almost done. To set all the generated classes in your project classpath enter this configuration to your pom.xml file
<build>
     <resources>
         <resource>
             <directory>src/main/resources</directory>
         </resource>
         <resource>
             <directory>target/generated-sources/axis2/wsdl2code/resources</directory>
         </resource>
     </resources>
...
</build>
…
<dependencies>
     <dependency>
         <groupId>org.apache.axis2</groupId>
         <artifactId>axis2</artifactId>
         <version>1.6.2</version>
     </dependency>
     <dependency>
         <groupId>org.apache.xmlbeans</groupId>
         <artifactId>xmlbeans</artifactId>
         <version>2.5.0</version>
     </dependency>
     <dependency>
         <groupId>org.apache.ws.commons.axiom</groupId>
         <artifactId>axiom-api</artifactId>
         <version>1.2.13</version>
     </dependency>
     <dependency>
         <groupId>org.apache.ws.commons.axiom</groupId>
         <artifactId>axiom-impl</artifactId>
         <version>1.2.13</version>
     </dependency>
     <dependency>
         <groupId>wsdl4j</groupId>
         <artifactId>wsdl4j</artifactId>
         <version>1.6.2</version>
     </dependency>
     <dependency>
         <groupId>org.apache.neethi</groupId>
         <artifactId>neethi</artifactId>
         <version>3.0.2</version>
     </dependency>
     <dependency>
         <groupId>org.apache.axis2</groupId>
         <artifactId>axis2-transport-local</artifactId>
         <version>1.6.1</version>
     </dependency>
     <dependency>
         <groupId>org.apache.axis2</groupId>
         <artifactId>axis2-transport-http</artifactId>
         <version>1.6.1</version>
     </dependency>
     <dependency>
         <groupId>org.apache.ws.commons.schema</groupId>
         <artifactId>XmlSchema</artifactId>
         <version>1.4.7</version>
     </dependency>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.10</version>
     </dependency>
 </dependencies>
  • run mvn eclipse:eclipse
  • Refresh Eclipse project

Using Unit Test for testing client

Now let’s test our generated client we will use unit tests and *Stub class – this is the code that connects to the WebService.
Create a class that will contain methods annotated with @Test annotation. Look at our example code and the comments:

@Test
public void testGetHighScoresForNickname() {
     
   // Use *Stub class to create WebService client
   HighScoreServiceStub stub = null;
   try {
       stub = new HighScoreServiceStub();
   } catch (AxisFault e) {
       e.printStackTrace();
       Assert.fail();
    }

   Assert.assertNotNull(stub);

   // Let's create request document - using axis2 generated classes     
   GetHighScoresForNicknameDocument requestDoc = GetHighScoresForNicknameDocument.Factory.newInstance();
     
   // Create request - class generated by axis2
   GetHighScoresForNickname request = GetHighScoresForNickname.Factory.newInstance();
     
   // Set place a nickname in the request
   request.setNickname("person3");
     
   // Place a request in the request document
   requestDoc.setGetHighScoresForNickname(request);
     
   // Create a response document - class generated by axis2
   GetHighScoresForNicknameResponseDocument responseDoc = null;
    try {
       // Call a WebService operation - getHighScoresForNickname
       responseDoc = stub.getHighScoresForNickname(requestDoc);
    } catch (RemoteException e) {
       e.printStackTrace();
       Assert.fail();
    }
     
    Assert.assertNotNull(responseDoc);
     
   // Get the response from the response document
   GetHighScoresForNicknameResponse response = responseDoc.getGetHighScoresForNicknameResponse();
     
    Assert.assertNotNull(response);
     
   // Get results array from the response object - HighScore class is generated by axis2 and represents highscore data
   HighScore[] result = response.getReturnArray();
     
    Assert.assertNotNull(result);
    Assert.assertTrue(result.length > 0);
     
   // Display the results     
   for (HighScore hs : result) {
       System.out.println(hs.getNickname() + " scores " + hs.getScore());
    }
}

As you can see calling WebService operations is easy with the generated *Stub class.
Run mvn test to test your solution.

Image may be NSFW.
Clik here to view.

Download this sample code here.

The post Axis 2 client appeared first on Programmer's lounge.


Viewing all articles
Browse latest Browse all 9

Trending Articles