Tuesday, April 21, 2009

GWT, Spring 3.0 MVC, and REST on Google App Engine / Java - Part 1

When googleing how to integrate GWT and Spring/Spring mvc, the most popular answer is to use the GWT- Widget/Server Library. The solutions are wrapping pojos as an RPC service or for better performance taking a spring controller and adding RPC functionality by extension.

With the intoduction of REST features in Spring MVC 3.0, this should make integration between GWT and Spring more natural. This is how to get it all working together on Google App Engine / Java.

First use Spring 3.0 M2. To include this in maven


<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.spring-library</artifactId>
<type>libd</type>
<version>3.0.0.M2</version>
</dependency>



Add the milestone repository
SpringSource Enterprise Bundle Repository - External Bundle Milestones
http://repository.springsource.com/maven/bundles/milestone
Note: ensure your using asm 2.2.3

Now in the web application you need to create the REST support with MVC. The most important feature added is the ContentNegotiatingViewResolver, this will determine which view to present from Accept header or extension, and from media type can resolve appropriate view. Probably the more important type will be for JSON support. In the blog post from Spring about REST in Spring 3 they hint at the JacksonJsonView and its inclusion in WebFlow or Spring Js, and at the time of this posting, unavailable. Anyways.. I really don't care to add web flow, or spring js to my project when using GWT. The project json-lib-ext-spring : details provides a json view that works seamlessly. Ensure that you exclude all spring libraries in your maven dependency.

In you spring configuration , it will look like this now..


<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>





<bean id="greet" class="path.to.GreetJsonView"/>



In the GreetJsonView just :

import net.sf.json.spring.web.servlet.view.JsonView;
public class GreetJsonView extends JsonView {
}

In the future it should be as easy and changing the class that you extend when spring releases a proper JSON view.

@Controller
@RequestMapping("/greet")
public class GreetController {

@RequestMapping(value = "greet", method = RequestMethod.GET)
public String list(ModelMap modelMap) {
modelMap.put("hello","REST rocks");

return "greet";
}
}

Now when you go to /path/greet it will display the jsp or if you go to /path/greet.json it will return the json string.

For additional details on how to get Spring MVC started and configured -
The most helpful is the pet clinic demo for Spring 3 M2 found in the subversion repository.

AtomView and REST Support

Example program called Spring Finance

Thats it for part 1, in my next post, I will show how to get GWT working with your spring mvc back end through REST calls, and putting it all together to get it working nicely on GAE/J

8 comments:

  1. Thanks Cheolho, nice post. Can't wait for part 2.

    I am just getting going with a project using GWT, JSON and Spring 3 MVC and would be interested in what libraries you use.

    You might be interest in a demo that Rob Harrop did at a recent Spring User Group meeting, there isn't a blog but the source is at http://github.com/robharrop/restlist/tree/master

    Regards,
    Dave

    ReplyDelete
  2. hey dave,
    thanks... ill definitely check out that project

    ReplyDelete
  3. If anyone is interested, I have a post about getting a simple Spring 3 Hello World up on GAE from scratch: http://bit.ly/10T8pd

    ReplyDelete
  4. Nice. Like Dave, I can't wait for part 2 :)

    Denis.

    ReplyDelete
  5. Hello does this idea work? None of my filters are working when I add JsonView to the controller.

    OpenEntityManagerInViewFilter
    httpMethodFilter (This is required for spring 3.0 rest)

    I get following error
    java.lang.ClassCastException: org.springframework.web.filter.HiddenHttpMethodFil
    ter
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(Applicatio
    nFilterConfig.java:255)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(Applica
    tionFilterConfig.java:397)
    at org.apache.catalina.core.ApplicationFilterConfig

    ReplyDelete
  6. Great article. I can't wait for part 2 with GWT/REST/Spring3 integration.

    ReplyDelete
  7. may i know for the guide are you using mvn archetype:generate -DarchetypeCatalog=http://www.mvnsearch.org/maven2 (struct2) as base project?

    ReplyDelete
  8. The GWT Widget/Server library doesn't work. It says
    Unable to connect to database server

    ReplyDelete