Using JAX-RS (RESTEasy) with CDI (Weld) in Tomcat 7

Anthony O. picture Anthony O. · Jun 14, 2011 · Viewed 7k times · Source

In a JSF 2.0 application (running on Tomcat 7 and using weld 1.1.1.Final), I want to propose my user to download some binary files (.doc, .pdf etc).

In order to fulfil that need, I want to use a JAX-RS (RESTEasy 2.2.0.Final) resource bean (annotated with @Path). The problem is that inside that bean, I want to call a service from a field annotated with @Inject annotation.

Actually, like a weld user trying a similar thing I've got a NullPointerException : Weld doesn't inject me my service.

So I read a post on JBoss community wiki talking about how to integrate RESTEasy with CDI so I've made my maven war project depend on org.jboss.resteasy:resteasy-cdi and here is my web.xml :

<!-- Weld -->
<resource-env-ref>
    <description>Object factory for the CDI Bean Manager</description>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<filter>
    <filter-name>ConversationPropagationFilter</filter-name>
    <filter-class>org.jboss.weld.servlet.ConversationPropagationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ConversationPropagationFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

<!-- Resteasy -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<context-param>
    <param-name>resteasy.injector.factory</param-name>
    <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

The problem now is that when my application bootstraps, I've got that exception :

java.lang.RuntimeException: Unable to instantiate InjectorFactory implementation.
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:141)
    at org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap.contextInitialized(ResteasyBootstrap.java:28)
...
Caused by: java.lang.NullPointerException
    at org.jboss.resteasy.cdi.CdiInjectorFactory.lookupBeanManager(CdiInjectorFactory.java:116)
    at org.jboss.resteasy.cdi.CdiInjectorFactory.<init>(CdiInjectorFactory.java:41)
...
    at java.lang.Class.newInstance(Class.java:308)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:133)

And when I remove the resteasy.injector.factory context-param, I've got an NPE when trying to get my service from the field variable annotated with @Inject ...

Does anybody knows how to configure RESTEasy beans to be managed by Weld (and make possible injection in JAX-RS resources) ?

Answer

Jozef Hartinger picture Jozef Hartinger · Jun 21, 2011

This has been fixed in RESTEasy trunk. See https://issues.jboss.org/browse/RESTEASY-558 for details.