Chapter 35. Spring Integration
RESTEasy integrates with Spring 2.5. (We are interested in other forms of Spring integration, and encourage you to contribute.)
35.1. Basic Integration
For Maven users, you must use the resteasy-spring artifact. Otherwise, the jar is available in the downloaded distribution.
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-spring</artifactId> <version>whatever version you are using</version> </dependency>
RESTEasy includes its own Spring
ContextLoaderListener
. This registers a RESTEasy-specific BeanPostProcessor
that processes JAX-RS annotations when a bean is created by a BeanFactory
. This means that RESTEasy automatically scans for @Provider
and JAX-RS resource annotations on your bean class and registers them as JAX-RS resources.
You will need to alter your
web.xml
file:
<web-app> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class>org.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <listener> <listener-class>org.resteasy.plugins.spring.SpringContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class>org.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
The
SpringContextLoaderListener
must be declared after ResteasyBootstrap
because it uses ServletContext attributes initialized by ResteasyBootstrap
.
If you do not use a Spring
ContextLoaderListener
to create your bean factories, you can manually register the RESTEasy BeanFactoryPostProcessor
by allocating an instance of org.jboss.resteasy.plugins.spring.SpringBeanProcessor
. You can obtain instances of a ResteasyProviderFactory
and Registry
from the ServletContext
attribute, org.resteasy.spi.ResteasyProviderFactory and org.resteasy.spi.Registry
(the fully-qualified name String of these classes). There is also org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware
, which automatically injects references to the Registry
and ResteasyProviderFactory
from the Servlet Context, assuming that you have used RestasyBootstrap
to bootstrap RESTEasy.
RESTEasy Spring integration supports both singletons and the prototype scope. It handles injecting
@Context
references. However, constructor injection is not supported, and when the prototype scope is used, RESTEasy injects any @*Param
-annotated fields and setters before the request is dispatched.
Note
You can only use automatically proxied beans with RESTEasy Spring integration. Hand-coded proxying (that is, with
ProxyFactoryBean
) will have undesirable effects.