Schlagwort-Archive: SOA

Creating a web service with Spring WS using JAXB2 marshalling

In the following the steps are shown how to create a simple web service with the Spring Web Service Framework using a JAXB2 marshalling. The example web service produces a list of random numbers. The amount of random numbers generated is specified in the request.


  • Eclipse Helios (3.6)
  • Installed m2eclipse plugin – this is not absolutely necessary, if you don’t want to use it, you can perform the Maven steps on the command line

First, we create a Maven project using the Spring WS archetype.

86a6a511406bc0f59463 221db397f3 c9807cc54e

Next, we change the dependency versions in the generated POM file to the current stable version (1.5.9) of Spring WS. Additionally we’re using Java version 1.5 and add the Jetty plugin for testing the service later.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
  <name>randomnumbers Spring-WS Application</name>

To let Eclipse know about the changes in the POM, we update the project configuration by right-clicking on the project -> Maven -> Update Project Configuration.

Next, we need to create an XML Schema for our service. Therefore we create a folder „xsd“ within src/main/webapp/WEB-INF and create the following „randomnumbers.xsd“ in there. The data structure itself is very simple. It contains two elements, the RandomNumbersRequest with the amount parameter and the RandomNumbersResponse containing a list of numbers.

Note, that the element names conform to the Spring naming conventions, i.e. the suffixes „Request“ and „Response“.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="" targetNamespace=""
 xmlns:tns="" elementFormDefault="qualified">

 <element name="RandomNumbersRequest">
        <element name="amount" type="int" />

  <element name="RandomNumbersResponse">
        <element name="number" type="int" minOccurs="1" maxOccurs="unbounded"/>

From this XSD the interface classes for the request and response can be generated. These classes will be used by the JAXB2 marshaller to convert the web service requests and responses from/to XML. The code generation is configured by adding the appropriate build plugin in pom.xml:


To actually generate code, run the generate-source target. This can be done in Eclipse by running the project as Maven build or on the command line:

mvn generate-sources

Afterwards, create the folder src/main/java to be able to implement the actual web service endpoint. Afterwards, refresh the project in Eclipse and update the Maven project configuration as before (Right-click the Project -> Maven -> Update Project Configuration).

Next, create the class RandomNumbersEndpoint as a subclass of AbstractMarshallingPayloadEndpoint that contains the implementation of the service:


package com.dkaedv.testws.randomnumbers;

import org.springframework.oxm.Marshaller;

import com.dkaedv.testws.randomnumbers.RandomNumbersRequest;
import com.dkaedv.testws.randomnumbers.RandomNumbersResponse;

public class RandomNumbersEndpoint extends AbstractMarshallingPayloadEndpoint {

  public RandomNumbersEndpoint(Marshaller marshaller) {

  protected Object invokeInternal(Object request) throws Exception {
    RandomNumbersRequest rnReq = (RandomNumbersRequest) request;
    RandomNumbersResponse response = new RandomNumbersResponse();

    int n = rnReq.getAmount();

    for (int i = 0; i < n; i++) {
      int randomNumber = (int) (Math.random() * 1000);

    return response;

As the final step, we need to add the appropriate configuration to spring-ws-servlet.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""

  <!-- Publish the WSDL
         at http://localhost:8080/randomnumbers/randomNumbersService/randomNumbers.wsdl -->
  <bean id="randomNumbers" class="">
    <property name="schema" ref="schema" />
    <property name="portTypeName" value="RandomNumbersPT" />
    <property name="locationUri" value="http://localhost:8080/randomnumbers/randomNumbersService/" />

  <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
    <property name="xsd" value="/WEB-INF/xsd/randomnumbers.xsd" />

  <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
    <property name="schema" value="/WEB-INF/xsd/RandomNumbers.xsd"/>

  <!-- Endpoint Definiton -->
  <bean id="randomNumbersEndpoint" class="com.dkaedv.testws.randomnumbers.RandomNumbersEndpoint">
    <constructor-arg ref="marshaller" />

  <!-- Routing -->
  <bean class="">
    <property name="mappings">
        <prop key="{}RandomNumbersRequest">randomNumbersEndpoint</prop>
    <property name="interceptors">
      <bean class="" />

That’s it. You can run the project using the Maven target „jetty:run“, e.g. on the command line:

mvn jetty:run

The WSDL should now be accessible at the address http://localhost:8080/randomnumbers/randomNumbersService/randomNumbers.wsdl. Note, that the WSDL was generated by Spring from the XML Schema created earlier – a manual creation is not required.

Of course you can now access and test the web service by using soapUI for example.