Quantcast
Channel: Programmer's lounge » maven 3
Viewing all articles
Browse latest Browse all 9

Axis2 WSDL2Java approach

$
0
0

In this tutorial we are going to create a WebService based on Axis2 wsdl2java approach. A WSDL file will be taken from our previous tutorial Axis2 Java2WSDL approach

In this tutorial we are going to discuss following topics:
- Setting up Maven 3 Axis2 project in Eclipse
-
Generating service classes and service.xml file from WSDL file
- Service implementation
- Building AAR archive and deploying on Axis2 engine

Setting up Maven 3 Axis2 project in Eclipse

We assume that you have maven and eclipse configured to work together. If no follow our tutorials where we show how to do it quickly – Maven 3 installation and configuration and Maven 3 Eclipse plugin setup

In order to setup a project for this tutorial 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 service classes and service.xml file from WSDL file

In order to generate service classes and a service.xml file follow this steps:

• Copy HighScoreService.wsdl (you can take it from Axis2 Java2WSDL 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.1</version>
   <executions>
       <execution>
           <goals>
               <goal>wsdl2code</goal>
           </goals>
           <configuration>
               <packageName>com.itcuties.ws</packageName>
               <wsdlFile>src/main/resources/wsdl/HighScoreService.wsdl</wsdlFile>
               <databindingName>xmlbeans</databindingName>                
               <syncMode>sync</syncMode>
               <unpackClasses>true</unpackClasses>
               <generateServerSide>true</generateServerSide>
               <generateServicesXml>true</generateServicesXml>
           </configuration>
       </execution>
   </executions>
</plugin>

• run mvn generate-sources
• Java classes are generated and can be found in target\generated-sources\axis2\wsdl2code\src
services.xml file is being generated in target\generated-sources\axis2\wsdl2code\resources
• Copy target\generated-sources\axis2\wsdl2code\resources\services.xml to src\main\resources directory
• Change configuration of axis2-wsdl2code-maven-plugin to generate service interface class

<plugin>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
   <version>1.6.1</version>
   <executions>
       <execution>
           <goals>
               <goal>wsdl2code</goal>
           </goals>
           <configuration>
               <packageName>com.itcuties.ws</packageName>
               <wsdlFile>src/main/resources/wsdl/HighScoreService.wsdl</wsdlFile>
               <databindingName>xmlbeans</databindingName>                
               <syncMode>sync</syncMode>
               <unpackClasses>true</unpackClasses>
               <generateServerSide>true</generateServerSide>
               <generateServicesXml>true</generateServicesXml>
               
               <!-- Add this parameter to generate service interface -->
               <generateServerSideInterface>true</generateServerSideInterface>
               
           </configuration>
       </execution>
   </executions>
</plugin>

• run mvn generate-sources

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>
</dependencies>

• run mvn eclipse:eclipse
• refresh eclipse project (hit F5)

Service implementation

In the last few steps we have generated a service.xml file from WSDL file now we are going to take care of the service implementation. Among generated classes we can find an interface com.itcuties.ws.HighScoreServiceSkeletonInterface. This interface contains all the methods that we need to implement in order to implement our webservice.

• Create a class that implements a com.itcuties.ws.HighScoreServiceSkeletonInterface

MyHighScoreServiceSkeleton.java

package com.itcuties.ws;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.itcuties.model.HighScore;
import com.itcuties.serivces.AddHighScoreDocument;
import com.itcuties.serivces.GetHighScoresDocument;
import com.itcuties.serivces.GetHighScoresForNicknameDocument;
import com.itcuties.serivces.GetHighScoresForNicknameResponseDocument;
import com.itcuties.serivces.GetHighScoresForNicknameResponseDocument.GetHighScoresForNicknameResponse;
import com.itcuties.serivces.GetHighScoresResponseDocument;
import com.itcuties.serivces.GetHighScoresResponseDocument.GetHighScoresResponse;
import com.itcuties.serivces.GetHighScoresSizeDocument;
import com.itcuties.serivces.GetHighScoresSizeResponseDocument;
import com.itcuties.serivces.GetHighScoresSizeResponseDocument.GetHighScoresSizeResponse;


public class MyHighScoreServiceSkeleton implements
       HighScoreServiceSkeletonInterface {

   /*
    * Warning!
    * There are two HighScore classes
    * 1) com.itcuties.model.HighScore
    *       this is model to store data on application side
    * 2) com.itcuties.serivces.xsd.HighScore used to serialize/deserialize
    *    data to/from WebService response/request
    */
   
   // A list to hold highScores
   private static List<HighScore> highScores = new ArrayList<HighScore>();

   static {
       highScores.add(new HighScore("person1", 100));
       highScores.add(new HighScore("person2", 90));
       highScores.add(new HighScore("person3", 80));
       highScores.add(new HighScore("person4", 70));
       highScores.add(new HighScore("person1", 60));
       highScores.add(new HighScore("person2", 50));
       highScores.add(new HighScore("person7", 45));
       highScores.add(new HighScore("person8", 40));
       highScores.add(new HighScore("person1", 35));
       highScores.add(new HighScore("person10", 30));
   }


   public void addHighScore(AddHighScoreDocument addHighScore) {
       //get HighScore object from request
       com.itcuties.serivces.xsd.HighScore tmp = addHighScore.getAddHighScore().getScore();
   
       //create HighScore object to save on application side
       HighScore hsToAdd = new HighScore(tmp.getNickname(), tmp.getScore());
           
       synchronized (HighScoreServiceSkeleton.class) {
           //index at which the new high score is to be inserted
           int addAtPosition = -1;
           
           //iterate through highScores list
           for (int i = 0; i < highScores.size(); i++) {
               HighScore hs = highScores.get(i);
               //compare highScores
               if (hsToAdd.getScore() >= hs.getScore()) {
                   //add before
                   addAtPosition = i;
                   break;
               }
           }            
           
           if (addAtPosition != -1) {            
               //there are worse scores, add before them
               highScores.add(addAtPosition, hsToAdd);
           } else {
               //very low score, add at the end
               highScores.add(hsToAdd);
           }
       }
   }

   public GetHighScoresResponseDocument getHighScores(GetHighScoresDocument getHighScores) {

       Collection<com.itcuties.serivces.xsd.HighScore> result = new ArrayList<com.itcuties.serivces.xsd.HighScore>();

       synchronized (HighScoreServiceSkeleton.class) {
           //iterate through highScores list
           for (HighScore hs : highScores) {

               //create HighScore object
               com.itcuties.serivces.xsd.HighScore tmp = com.itcuties.serivces.xsd.HighScore.Factory.newInstance();
               
               //set nickname and score on HighScore object
               tmp.setNickname(hs.getNickname());                
               tmp.setScore(hs.getScore());
               
               //add HighScore object to collection
               result.add(tmp);
           }
       }

       //create empty responseDocument
       GetHighScoresResponseDocument responseDoc = GetHighScoresResponseDocument.Factory
               .newInstance();
       
       //create empty response
       GetHighScoresResponse response = GetHighScoresResponse.Factory
               .newInstance();

       //set return value for response
       response.setReturnArray(result
               .toArray(new com.itcuties.serivces.xsd.HighScore[result.size()]));

       //add/set response to response document        
       responseDoc.setGetHighScoresResponse(response);
       
       return responseDoc;
   }

   public GetHighScoresForNicknameResponseDocument getHighScoresForNickname(GetHighScoresForNicknameDocument getHighScoresForNickname) {

       //get parameter from request
       String nickname = getHighScoresForNickname
               .getGetHighScoresForNickname().getNickname();
       
       Collection<com.itcuties.serivces.xsd.HighScore> result = new ArrayList<com.itcuties.serivces.xsd.HighScore>();

       if (nickname != null && !nickname.isEmpty()) {
           synchronized (HighScoreServiceSkeleton.class) {
               //iterate through highScores list in order to find results achieved by specified user
               for (HighScore hs : highScores) {
                   if (hs.getNickname().equalsIgnoreCase(nickname)) {
                   
                       //create HighScore object
                       com.itcuties.serivces.xsd.HighScore tmp = com.itcuties.serivces.xsd.HighScore.Factory
                               .newInstance();
                       
                       //set nickname and score on HighScore object
                       tmp.setNickname(hs.getNickname());
                       tmp.setScore(hs.getScore());
                       
                       //add HighScore object to collection
                       result.add(tmp);
                   }
               }
           }
       }

       //create empty responseDocument
       GetHighScoresForNicknameResponseDocument responseDoc = GetHighScoresForNicknameResponseDocument.Factory.newInstance();
       
       //create empty response
       GetHighScoresForNicknameResponse response = GetHighScoresForNicknameResponse.Factory.newInstance();

       //set return value for response
       response.setReturnArray(result.toArray(new com.itcuties.serivces.xsd.HighScore[result.size()]));

       //add/set response to response document        
       responseDoc.setGetHighScoresForNicknameResponse(response);
       
       return responseDoc;
   }

   public GetHighScoresSizeResponseDocument getHighScoresSize(GetHighScoresSizeDocument getHighScoresSize) {

       //create empty responseDocument
       GetHighScoresSizeResponseDocument responseDoc = GetHighScoresSizeResponseDocument.Factory
               .newInstance();
       
       //create empty response
       GetHighScoresSizeResponse response = GetHighScoresSizeResponse.Factory
               .newInstance();

       //set return value for response
       synchronized (HighScoreServiceSkeleton.class) {
           response.setReturn(highScores.size());
       }

       //add/set response to response document
       responseDoc.setGetHighScoresSizeResponse(response);
       
       return responseDoc;
   }

}

• We use a model class here. Here is its code as well

HighScore.java

package com.itcuties.model;

public class HighScore {
   // User nickname
   private String nickname;
   // User scores
   private int score;
   
   public HighScore() {
   }
   
   public HighScore(String nickname, int score) {
       this.nickname = nickname;
       this.score = score;
   }
   
   public String getNickname() {
       return nickname;
   }
   
   public void setNickname(String nickname) {
       this.nickname = nickname;
   }
   
   public int getScore() {
       return score;
   }
   
   public void setScore(int score) {
       this.score = score;
   }
   
}

• Last step is to configure our implementation class in the services.xml file. Open src\main\resources\services.xml file and change parameter to:

<parameter name="ServiceClass">com.itcuties.ws.MyHighScoreServiceSkeleton</parameter>

Building AAR archive and deploying on Axis2 engine

Now we are ready to build and deploy our service. We assume that you an Axis 2 deployment environment setup. If not – just follow our tutorial Axis2 Tomcat Eclipse setup

• Add axis2-aar-maven-plugin configuration to your pom.xml file

<plugin>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-aar-maven-plugin</artifactId>
   <configuration>                
       <servicesXmlFile>${basedir}/src/main/resources/services.xml</servicesXmlFile>
       <wsdlFile>${basedir}/src/main/resources/wsdl/HighScoreService.wsdl</wsdlFile>
   </configuration>
   <executions>
       <execution>
           <phase>package</phase>
           <goals>
               <goal>aar</goal>
           </goals>
       </execution>
   </executions>
</plugin>

• run mvn clean generate-sources package
• Everything should be compiled and packaged to JAR and AAR archive.
• copy HighScoreServiceServer-0.0.1-SNAPSHOT.aar to axis2\WEB-INF\services\ on your tomcat server
• start tomcat

Test your WebService. If you don’t know how to do it just take a look here

Download this sample code here.

The post Axis2 WSDL2Java approach appeared first on Programmer's lounge.


Viewing all articles
Browse latest Browse all 9

Trending Articles