Sunday, August 2, 2009

Spring Web Service

Spring-WS supports the contract-first development style that means we start with the WSDL contract and use Java to implement said contract.

We will write a sample code to demonstrate how to write contract-first Web services, that is, developing web services that start with the XML Schema/WSDL contract first followed by the Java code second.

Let us take an example where any service provider maintain user database and provides services for updating user information, providing user information (user profile) and authenticating user. Let say user profile has login id, first name, last name, email and address. We want to provide services like AuthenticateUser, UpdateUserProfile, GetUserProfile.

Let us follow the following steps -

1. So, first step would be to create XSD (let us name it as user.xsd). Go through the below schema, it is self-explanatory

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns=""

<element name="AuthenticateUser">
<element name="LoginId" type="string" />
<element name="Password" type="string" />

<element name="UpdateUserProfile">
<element name="Profile" type="tns:UserProfile" />
<element name="LoginId" type="string" />

<element name="GetUserProfile">
<element name="LoginId" type="string" />

<complexType name="UserProfile">
<element name="loginId" type="string" />
<element name="firstName" type="string" />
<element name="lastName" type="string" />
<element name="emailId" type="string" />
<element name="address" type="string" />

<element name="GetUserProfileResponse" type="tns:UserProfile"/>

<element name="AuthenticateUserResponse" type="int" />

<element name="UpdateProfileResponse" type="int" />


2. Now that the schema is created, we will generate supporting classes. We will use JAXB to generate the classes. Download JAXB and unzip it. Use the following command to generate the classes -

%JAXB_HOME%\bin\xjc.bat -d . -p test.schema ./config/user.xsd

The target package is test.schema and schema is under config directory. This would generate the classes based on above user schema.

OR we can use the following build.xml using "ant generate"

<?xml version="1.0"?>
<project name="spring-ws-user-ws-server" basedir="." default="default">
<path id="classpath">
<fileset dir="C:\jaxb-ri-20090206\lib">
<include name="**/*.jar"/>

<target name="generate">

<taskdef name="xjc" classname="" classpathref="classpath" />

<xjc destdir="./" package="test.schema" >
<schema dir="./config" includes="user.xsd"/>



3. Implementing the Endpoint - In Spring-WS, we implement Endpoints to handle the incoming XML messages. There are two flavors of endpoints: message endpoints and payload endpoints. Message endpoints give access to the entire XML message, including SOAP headers. Typically, the endpoint will only be interested in the payload of the message that is the contents of the SOAP body. In that case, creating a payload endpoint makes more sense.

To handle XML we can use various marshalling techniques like JAXB, Castor,XStream etc apart from JDOM, DOM, dom4j, XOM, SAX,and StAX. We will use JAXB marshalling technique.


import javax.xml.bind.JAXBElement;


import test.schema.AuthenticateUser;
import test.schema.GetUserProfile;
import test.schema.ObjectFactory;
import test.schema.UpdateUserProfile;
import test.schema.UserProfile;
import test.service.IUserService;

public class UserEndPoint {

IUserService userService;

private ObjectFactory objectFactory = new ObjectFactory();

public UserEndPoint(IUserService userService) {
this.userService = userService;

@PayloadRoot(localPart = "AuthenticateUser", namespace = "")
public JAXBElement<Integer> authenticateUser(AuthenticateUser request) {
System.out.println(request.getLoginId() + " " + request.getPassword());
int result = userService.authenticateUser(request.getLoginId(), request
return objectFactory.createAuthenticateUserResponse(result);

@PayloadRoot(localPart = "UpdateUserProfile", namespace = "")
public JAXBElement<Integer> updateUserProfile(UpdateUserProfile request) {
int result = userService.updateUserProfile(request.getLoginId(),
return objectFactory.createUpdateProfileResponse(result);

@PayloadRoot(localPart = "GetUserProfile", namespace = "")
public JAXBElement<UserProfile> getUserProfile(GetUserProfile request) {
UserProfile userProfile = userService.getUserProfile(request
return objectFactory.createGetUserProfileResponse(userProfile);

4. We have injected userService to above end point, the service interface looks like -

package test.service;

import test.schema.UserProfile;

public interface IUserService {
public int authenticateUser(String loginId, String password);

public int updateUserProfile(String loginId, UserProfile profile);

public UserProfile getUserProfile(String loginId);

5. Routing the Message to the Endpoint - I have used annotation to configure Endpoint and PayloadRoot. See @Endpoint and @PayloadRoot. let us define applicationContext-ws.xml

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

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

<bean id="userEndPoint" class="">
<constructor-arg ref="userService"/>

<oxm:jaxb2-marshaller id="marshaller" contextPath="test.schema"/>

<bean id="annotationMapping"


Also we define applicationContext.xml where we define the userService bean

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


<bean id="userService" class="test.service.UserService" />

6. Publishing the WSDL - we don't have to write a WSDL; Spring-WS can generate itself. This is how ws-servlet.xml looks like

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

<bean id="user" class="">
<property name="schema" ref="schema"/>
<property name="portTypeName" value="User"/>
<property name="locationUri" value="/userService"/>


- The bean id determines the URL where the WSDL can be retrieved. Here the bean id is user that means the WSDL can be retrieved as user.wsdl in the servlet context. The full URL will be like http://localhost:8080/UserService/user.wsdl.

- The schema property refers to the user schema defined in point 1. We will place the schema in the WEB-INF directory of the application.

- We will define the WSDL port type to be User

- We set the location where the service can be reached: /UserService/

7. Now let us write web.xml, which looks like

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns=""
<display-name>Spring WS</display-name>



8. Now let us deploy the web application.
- Copy user.xsd, web.xml, applicationContext-ws.xml, applicationContext.xml, ws-servlet.xml under /WEB-INF/
- Copy all required libs under WEB-INF/lib - spring-ws-1.5.6-all.jar, spring-ws-core-1.5.6.jar, spring-webmvc.jar, spring-oxm-1.5.6.jar, spring.jar, commons-logging-1.0.4.jar, wsdl4j-1.6.1.jar, XmlSchema-1.4.3.jar
- Copy the UserEndPoint class, all supporting classes and service classes under WEB-INF/class and restart tomcat

Create WAR and deploy under Tomcat. We have successfully deployed the webservice. Check the WSDL using http://localhost:8080/UserService/user.wsdl

We will next write the dummy client. But after a short break :)

1 comment:

Rick said...

Thanks much for the clear and simple explanation.