Search code examples
javatomcatjbosstomcat7resteasy

resteasy-cdi breaks deployment to Tomcat 7


I am developing a simple REST server using resteasy 3.0.6.Final, which is deployed to tomcat 7.0.50.

According to the official doc, there are basically two ways to do it:

  1. using ServletContainerInitializer, which is implemented in the package resteasy-servlet-initializer.

  2. using web.xml and servlet dispatcher.

I have tried the approaches and they both worked for me.

Now I want to add CDI support using WELD, and to do that I need to add the dependency

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-cdi</artifactId>
    <version>3.0.6.Final</version>
</dependency>

to my war/WEB-INF/lib.

However, this breaks the deployment. Tomcat shows only the following error message in catalina.out:

Jan 20, 2014 10:24:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
Jan 20, 2014 10:24:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/storage] startup failed due to previous errors

There are many suggestions online about removing resteasy-cdi. So

  1. Why is resteasy-cdi breaking the deployment? Is there a ticket documenting the problem?
  2. How can I integrate resteasy with WELD otherwise?

Thanks for any help.

Update 1

After a session of debugging, the issue turns out to be very simple: resteasy-cdi needs a concrete CDI implementation to work, which means I should include

    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
    </dependency>

in my build.

Another note is on configuring WELD mentioned in the official doc. When I include these two listeners in my web.xml,

<listener>
   <listener-class>org.jboss.weld.servlet.WeldInitialListener</listener-class>
</listener>
<listener>
   <listener-class>org.jboss.weld.servlet.WeldTerminalListener</listener-class>
</listener>

I got the following error in my localhost.log

Jan 20, 2014 11:55:40 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.jboss.weld.servlet.WeldInitialListener
java.lang.IllegalStateException: Singleton is not set. Is your Thread.currentThread().getContextClassLoader() set correctly?

I am not sure if the doc is wrong, but replacing the listeners with

<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

did the trick and the war file deployed successfully.


Solution

  • I've asked the Singleton is not set exception in the official weld forum and it is a bug in the docs. As you did, you should use the 'original' org.jboss.weld.environment.servlet.Listener.