Over a million developers have joined DZone.

Integrating JAX-RS and Spring MVC

DZone's Guide to

Integrating JAX-RS and Spring MVC

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 */
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

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}