Navigation
Tags
Categories
Powered by Squarespace

Entries in Java (1)

Friday
27Nov2009

JSON and REST with Spring 3.0

The version 3 of the Spring framework, which is available as ReleaseCandidate2 now, brings some nice new features for developers who want to add RESTful web services to a Java web project. I feel it offers a real alternative to the different JAX-RS implementations, because you need less code and less (but still a bit, it's spring after all :)) configuration of xml files, especially if you use spring anyway.

Interesting enough, Springsource (the company behind spring) decided to add this feature to the spring-mvc project, not spring-ws. The reason behind this is that "There were a number of technical issues [...] most importantly the approach felt 'clunky' and unnatural for a developer who was already used to Spring MVC 2.5." (as stated here, which is a nice introduction to the new REST features in spring 3 btw).

Version 3.0 of spring-mvs adds, among other things, a view to render JSON representations of your data objects: the JacksonJsonView. Regarding the growing number of rich javascript frontends built with jQuery et al, this of particular interest. As the name suggests, this view relies on the Codehaus Jackson JSON processor, a parser and generator which is especially known to be really fast. While there were some third-party solutions to add JSON support to spring based webapps before (http://spring-json.sourceforge.net for example, to name just one of them), it is nice to have this feature in the framework itself.

To use the JacksonJsonView, you need the current spring-mvc and spring-aop artifacts (3.0.0.RC2 by the time of this writing), and the jackson mapper 1.3.0. The maven dependencies are:

<dependencies>
  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-webmvc</artifactId>
  	<version>3.0.0.RC2</version>
  </dependency>
  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-aop</artifactId>
  	<version>3.0.0.RC2</version>
  </dependency>
  <dependency>
  	<groupId>org.codehaus.jackson</groupId>
  	<artifactId>jackson-mapper-asl</artifactId>
  	<version>1.3.0</version>
  </dependency>
</dependencies>

As spring 3 has not yet been released, (the current version is RC2), you need to add the spring maven repo to your pom file as well:

<repositories>
	<repository>
		<id>springsource maven repo</id>
		<url>http://maven.springframework.org/milestone</url>
	</repository>
</repositories>

Spring 3 adds the possibility to use content negotiation by URI and Accept HTTP header to determine which representation to return via the ContentNegotiatingViewResolver, which is a elegant way to support multiple data representations(JSON, XML, RSS, Atom...). But to keep things simple I assume we only want to render JSON and are using the XmlViewResolver. (You can find more on views and view resolving in spring mvc in the spring documenation).

This means your servlet application context might look like this:

<beans>
<!!-- Search for annotated controllers in the package net.mgroh.www.foo.ctrl -->
<context:annotation-config />
<context:component-scan base-package="net.mgroh.www.foo.ctrl" />

<!!-- Map handler methods on HTTP paths with RequestMapping etc -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!!-- Use the XmlViewResolver -->
<bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"/>
</beans>

And you have the following in a file named views.xml:

<beans>
<bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</beans>

You can then implement a Controller serving a JSON representation of an object Foo like this:

@Controller
public class FooController {

	@RequestMapping(value = "/foo/{fooid}", method = RequestMethod.GET)
	public ModelAndView findFoo(@PathVariable String fooid) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("jsonView");
		modelAndView.addObject(new Foo());
		return modelAndView;
}

And that's all what you need to implement a RESTful Web Service with Spring MVC.

In Spring 3 there is a lot more which makes it a easy to use (if you know spring, obviously) and (relatively) lightweight framework for implementing RESTful Web Services in the Java world. A good starting point is this article I mentioned before and the Spring documentation of course.