Saturday, September 19, 2009

RESTful Web Services with Spring 3 Experience

Arjen Poutsma introduced REST support in Spring 3 through his two blogs REST in Spring 3: @MVC and REST in Spring 3: RestTemplate. Follow his instructions and Spring 3 PetClinic sample application, I got RESTful Web Service working in my Open Toast Project.

Just to provide a very simple service "get member object by member id", I created a new Web bundle project "" by copying "org.opentoast.web" project. There is only one Java class in new project, MemebrController:

public class MemberController
    protected final Log log = LogFactory.getLog(getClass());
    private MemberManager memberManager;
    public MemberController(MemberManager memberManager){
        this.memberManager = memberManager;
    @RequestMapping(value="/members/{memberId}", method = RequestMethod.GET)
    public ModelAndView getMember(@PathVariable("memberId") Long id) {
        Member m = memberManager.getMemberById(id);
        ModelAndView mav = new ModelAndView("member");
        mav.addObject("member", m);
        return mav;

It provides one RESTful service in the method getMember(). It is annotated by @RequestMapping with URI template /members/{memberId}. Using @PathVariable("memberId"), the request member id is passed to parameter id. So with the injected MemberManager, we can get the Member object by id and pass to returned ModelAndView object. The view name is "member", and result member object is put into model with key "member" as well.

To setup REST support, there are few changes in config file module-context.xml:

<beans xmlns="" xmlns:xsi=""
  xmlns:p="" xmlns:aop=""
  xmlns:context="" xmlns:jee=""

 <context:component-scan base-package=""/>

 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
 <bean id="member" class="org.springframework.web.servlet.view.xml.MarshallingView">
  <property name="contentType" value="application/"/>
  <property name="marshaller" ref="marshaller"/>
  <property name="modelKey" value="member"/>

 <oxm:jaxb2-marshaller id="marshaller">
  <oxm:class-to-be-bound name="org.opentoast.domain.Member"/>


Since we only want to support REST service with customized content type, we don’t need ContentNegotiatingViewResolver , so only one view resolver is set in the context: org.springframework.web.servlet.view.BeanNameViewResolver. It will resolve view named "member" to bean "member", which is an instance of org.springframework.web.servlet.view.xml.MarshallingView. The content type is "application/", modelKey is "member" and reference to jaxb2 marshaller bean "marshaller".

The web.xml sets servlet with org.springframework.web.servlet.DispatcherServlet, named opentoastrest, and maps to /*:

<web-app xmlns="" xmlns:xsi=""

 <display-name>Open Toast RESTful</display-name>





You can check out the code from Google Code by running

svn checkout opentoast

Then inside opentoast folder, run

mvn package

to build the whole application. Copy the par file at opentoast/org.opentoast.par/target/OpenToast.par to springsource dm Server pickup folder, and start dm Server.

To test the RESTful Web Service, here is a client example code:

package org.opentoast.client;

import java.util.HashMap;
import java.util.Map;

import org.opentoast.domain.Member;
import org.springframework.context.ApplicationContext;
import org.springframework.web.client.RestTemplate;

public class RestClient
    RestTemplate restTemplate;
    public RestClient(RestTemplate template)
        this.restTemplate = template;
    public void testMember()
        Map<String, String> vars = new HashMap<String, String>();
        vars.put("memberId", "1");
        Member result = restTemplate.getForObject(
                Member.class, vars);
    public static void main(String[] args)
        ApplicationContext ac = new ClassPathXmlApplicationContext(
        RestClient client = (RestClient)ac.getBean("restClient");
With xml config file /org/opentoast/client/appContext.xml:

<beans xmlns=""

 <bean id="restClient" class="org.opentoast.client.RestClient">
  <constructor-arg ref="restTemplate"/>

 <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
  <property name="messageConverters">
    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
     <constructor-arg ref="marshaller"/>
     <property name="supportedMediaTypes">
       <bean class="org.springframework.http.MediaType">
        <constructor-arg value="application"/>
        <constructor-arg value=""/>

 <oxm:jaxb2-marshaller id="marshaller">
  <oxm:class-to-be-bound name="org.opentoast.domain.Member"/>
Run this test client program and you will get print out:

[Member 1: Jane Smith]

One big question I have now is "Do I need to expose my rich domain model as REST object model?"

JAXB2 marshaller requires Member class to be annotated with @XmlRootElement. And my domain model class Member is already annotated with persistent API, so now it looks like:

@Table(name = "MEMBER")
public class Member extends BaseEntity

If I don't want REST client to see my rich domain model, I have to use DTO model, and map between those two models. That will defeat the purpose of Domain Driven Design. If I expose my rich domain model classes to client, it will be more problematic when I try to use GWT as client application running inside browser. So far I haven't found a good solution for GWT + REST.

No comments: