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.

Monday, September 14, 2009

Release s2dm Maven Plugin 0.0.3

Open Toast Project Maven Plugin for Spring Source dm Server Development 0.0.3 was released.

After Spring Bundlor released 1.0M5, I tried and got it working with my project.

Still have trouble with M2Eclipse, the Maven Dependencies classpath of web project doesn't include depending projects in workspace (domain and service projects). So I have to manually add those two projects to web project Java Build Path.

The 0.0.3 release can be used to set up Eclipse workspace, and build par.

To use that plugin, in parent pom.xml, setup plugin repository to

  <!-- Maven 2 Plugin for S2DM -->
  <!-- SpringSource milestone -->
   <name>SpringSource Enterprise Bundle Repository</name>


So go to project root directory /opentoast, and type

mvn org.opentoast:maven-s2dm-plugin:0.0.3:eclipse

It will generate all eclipse setting files, such as /project, .classpath, .settings/org.eclipse.wst.common.project.facet.core.xml, etc. It also generates a dummy MANIFEST.MF file at generated/META-INF/MANIFEST.MF under each bundle project folder. The trick is the .classpath will include the generated folder as classpathentry, so STS bundlor will automatically update it with settings.

To build the whole application into a par, just type mvn package under opentoast, it will use dm-par goal to package par artifact.

So far STS does not work with dm Server 2.0.0.M4, so I just use STS to edit code, and use command line to build par, then deploy to local running dm Server pickup folder. I will try to get the application running inside STS later. Hope dm Server and Spring 3 can be released before Christmas, so I can play with them during winter break.