Enterprise Integration Zone is brought to you in partnership with:

I'm Solomon Duskis, NYC consultant and a Java/J2EE guy. I work at Sungard Consulting Services in NYC. The postings on this site are my own and do not necessarily represent the positions, strategies or opinions of my employer. Solomon is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

Integrating JAX-RS and Spring MVC

11.06.2008
| 18305 views |
  • submit to reddit

Spring MVC and JAX-RS have a lot in common. I've been working on integrating those two technologies as part of the JBoss RESTEasy project, and have been pretty successful at it. My changes are all in the the RESTEasy subversion repository in the resteasy-spring package. Here are some of the enhancements that I've put in place:

1. RESTEasy can now be used with the Spring MVC DispatcherServlet. All you need to do is <import resource:"springmvc-resteasy.xml"/>. This has quite a few benefits:

o You can manage JAX-RS Resources along side SprngMVC Controllers, or Wicket Objects or Tapestry or Struts2 Actions. JAX-RS can be set up to handle XML and JSON interactions, and your favorite MVC framework can handle the HTML creation.

o Your JAX-RS resources can be full-fledged MVC Controllers by returning a Spring ModelAndView. It can be a JSP view, a Freemarker, XSLT or Velocity template, or an RSS view.

Take a look at my JUnit4 test case, specifically the getCustomRepresentation() method:

			/** WOOHOO!  SpringMVC ModelAndView in action */
@GET
@Produces("application/custom")
@Path("/custom-rep")
public ModelAndView getCustomRepresentation() {
return new ModelAndView("customView");
}

2. Spring managed Embedded servers.

o There's JettyLifeCycleManager which starts Jetty after a Spring application context is loaded, and stops jetty during application context shutdown. You can configure Jetty to point to a .war exploded directory or a .war file, and start your engines. Here's an example spring configuration and a Spring TestCase that uses it

o There's also a TJWS (Tiny Java Web Server) TJWSEmbeddedSpringMVCServerBean that likewise manages the web server's life-cycle in accordance with the Spring application context. All you need to do is point to a Spring xml configuration file and a port number, and it starts up a server with a SpringMVC DispatcherServlet pointing to your configuration. It's also managed by the Spring Application Context factory. Here's an example spring configuration and a test case that goes with it.

3. Springier Integration

o Resteasy supports Spring life-cycle management. You can create request or prototype beans and register them in Spring. James Strachan describes how he got Guice to inject custom annotations; I did the same thing for Spring to inject @Context member variables such as HttpHeaders (which creates a Proxy to an object that is aware of the HttpServletRequest).

o Various minor fixes in the resteasy code base that make common resteasy components friendly to a Spring environment. One such change was to implement equals and hashCode on the proxies produced by the RESTEasy Client Framework; for some reason, the Spring PersistenceAnnotationBeanPostProcessor needed it.

4. Easy testing. Take a look at the JUnit tests I described in the Embedded server notes. The tests don't know anything about how Services are implemented. They don't need know anything about which servers they hit.

I used RESTEasy because I was able to grok the code-base quicker than the other JAX-RS implementations. I'll be glad to discuss this integration with either end users or developers of other frameworks.

This integration will be put into an official release in about a week. Please feel free to email me about it.

From New York Java Consultant

Published at DZone with permission of Solomon Duskis, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)