Axis2 Java2WSDL approach

In this tutorial we are going to code a WebService based on Axis2 java2wsdl approach – exposing POJO as a WebService. If you haven’t do so yet configure your development environment based on Eclipse IDE. You might find those two tutorials very helpful:
- Eclipse Tomcat 7 Setup
- Axis2 Tomcat Eclipse setup
Our example is an implementation of a HighScore Service. It implements addHighScore, getHighScores, getHighScoresForNickname, getHighScoresSize operations. It might be used as a base for your highscore system if you are developing mobile games etc.
In this tutorial we are going to discuss following topics:
- Setting up Maven 3 Axis2 project in Eclipse
- HighScoreService POJO implementation
- Exposing POJO as a WebService
- Testing solution

Setting up Maven 3 Axis2 project in Eclipse


• In a Project Explorer window right-click and select New -> Project...
• Pick Maven Project and click Next
• On the Archetype screen select maven-archetype-quickstart and click Next
• Enter Group Id, Artifact Id and Package values
• Click Finish
Since we are not going to take any unit tests while working on that sample project you can follow those few steps to remove them.
• Open pom.xml and delete dependency with groupId of junit
• Delete classes from src/test/java directory


HighScoreService POJO implementation


We are going to implement out logic as a simple POJO. We need a class that will represent highscore data.

HighsScore.java

package com.itcuties.serivces;

public class HighScore {
	// User nickname
	private String nickname;
	// User scores
	private int 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;
	}	
}

This class represents one record in our HighScoreService. A record contains a user nickname and his score.
Our service logic is implemented as a simple POJO. Our logic implementation is pretty straightforward.

HighScoreService.java

package com.itcuties.serivces;

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

public class HighScoreService {

	// A list to hold highScores
	private static List<HighScore> highScores = new ArrayList<HighScore>();
	
	// Add highscore
	public void addHighScore(HighScore score) {
		System.out.println("{addHighscore} Adding: ["+score.getNickname()+","+score.getScore()+"]");
		
		highScores.add(score);
	}

	// Get highscores
	public List<HighScore> getHighScores() {
		System.out.println("{getHighScores}:: start");
		
		for (HighScore h: highScores)
			System.out.println("{getHighScores} Returning: ["+h.getNickname()+","+h.getScore()+"]");
		
		return highScores;
	}
	
	// Get highscores for user nickname
	public List<HighScore> getHighScoresForNickname(String nickname) {
		System.out.println("{getHighScoresForNickname}:: start");
		
		List<HighScore> userScores = new ArrayList<HighScore>();
		
		for (HighScore h: highScores)
			if (h.getNickname().equals(nickname)) {
				System.out.println("{getHighScoresForNickname} Returning: ["+h.getNickname()+","+h.getScore()+"]");
				userScores.add(h);
			}
		
		return userScores;
	}
	
	// Get the size of the scores list
	public int getHighScoresSize() {
		System.out.println("{getHighScoresSize}:: start");
		return highScores.size();
	} 
}

Take a look on highScores member of this class. It is a list that will hold data. It has to be static because when a request is processed by Axis a new instance of our class is created.

Exposing POJO as a WebService


We have our service Implemented as a POJO. Now let’s expose it as a WebService. We will use two maven plugins for that:
- axis2-java2wsdl-maven-plugin – to generate a WSDL file for our POJO class.
- axis2-aar-maven-plugin – to package our solution as an AAR archive to deploy it to Axis Web application.

axis2-java2wsdl-maven-plugin

Configure plugin in pom.xml file like this

	...
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.axis2</groupId>
				<artifactId>axis2-java2wsdl-maven-plugin</artifactId>
				<configuration>
					<className>com.itcuties.serivces.HighScoreService</className>
					<outputFileName>${project.build.directory}/HighScoreService.wsdl</outputFileName>
				</configuration>
				<executions>
					<execution>
						<phase>process-classes</phase>
						<goals>
							<goal>java2wsdl</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			...

We perform this step only to find out how will service namespace look like when the WSDL file is generated by Axis 2. We use this information to configure AAR plugin. WSDL is generated automatically by Axis 2 Servlet when AAR is deployed to the application.

axis2-aar-maven-plugin

Now we are going to configure AAR plugin to generate deployable archives. Provide following configuration in the pom.xml file

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

This configuration will read services.xml file and will package our service so that we can deploy it. Next step is to configure our service description – services.xml file.

services.xml

<?xml version="1.0" encoding="UTF-8"?>
<service name="HighScoreService">
    
    <description>
        Simple implementation of a HighScoreService.
    </description>
    
    <parameter name="ServiceClass">com.itcuties.serivces.HighScoreService</parameter>
    
    <operation name="addHighScore" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.itcuties.com/xsd">
		<actionMapping>http://serivces.itcuties.com/xsd/addHighScore</actionMapping>
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    </operation>
    
    <operation name="getHighScores" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.itcuties.com/xsd">
		<actionMapping>http://serivces.itcuties.com/xsd/getHighScores</actionMapping>
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    
    <operation name="getHighScoresForNickname" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.itcuties.com/xsd">
		<actionMapping>http://serivces.itcuties.com/xsd/getHighScoresForNickname</actionMapping>
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    
    <operation name="getHighScoresSize" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.itcuties.com/xsd">
		<actionMapping>http://serivces.itcuties.com/xsd/getHighScoresSize</actionMapping>
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    
</service>

namespace and actionMapping values are taken generated WSDL file. Very important things are the message receivers. If a method doesn’t return anything just like our addHighScore method a RPCInOnlyMessageReceiver should be used. When your method returns something use RPCMessageReceiver. It’s important to use appropriate message receiver otherwise you will end up with java.lang.UnsupportedOperationException: An access occurred that is not valid. :)

Testing solution


We use soapUI to test our work. You can find more information here.
• Navigate to your project main directory and enter mvn package command – this builds our project,
• Start your application server,
• Copy build result, the AAR file, to /WEB-INF/services directory of Axis 2 Web application – you just deployed your WebService,
• Navigate to http://localhost:8080/axis2 and click Services and then click on your service name – this displays WSDL generated by the axis application,
• Copy WSDL URL
• In the soapUI projects window click New soapUI Project option
• Enter WSDL URL in Initial WSDL field and click OK
• Test your WebService by calling every operation.

Download this sample code here.

5 Responses to "Axis2 Java2WSDL approach"

  1. Max Taylor says:

    Hi,

    Your code does not seem to generate the AAR file in the end. Everything else is fine.

    Just curious whether you have any ideas.

    Many thanks

    \MT

    Reply
    • itcuties says:

      This code was tested at the time of writing this tutorial. Can you specify what’s wrong with the AAR file?

      Reply
  2. Ashish Bhosle says:

    Good

    Reply
  3. Saurabh says:

    Hey m getting error in pom .xml at tag
    plugin execution not covered by lifecycle configuration

    Reply
  4. Saurabh says:

    error at execution tag

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply to Max Taylor Cancel reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>