0 Replies Latest reply: Apr 8, 2012 12:10 PM by Feng Jiang RSS

Inject doesn't work in RestEasy on tomcat7

Feng Jiang Newbie

Hi,

     My application is build with weld.1.1.5.Final + resteasy.2.3.2.Final on tomcat7.   Weld is work fine , but i cann't inject bean in the resteasy beans.  the return always is null. 

 

     Here is my code:

 

@Path("/message")
@RequestScoped
public class RestEasyTest {
          @Inject
          private WeldTest weldTest;
          @GET
          @Path("/{param}")
          public Response printMessage(@PathParam("param") String msg) {
                    String result = "Restful example : " + msg + weldTest.getWeld();
                    return Response.status(200).entity(result).build();
          }
}

 

      But  i can inject in other beans:

 

public class WeldTest {
          @Inject
          private CDITest cdiTest;
          public String getWeld(){
                    return " Weld " + cdiTest.test();
          }
}

 

     My web.xml

 

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">  
   <display-name>Web  example</display-name>

   <!--  Weld -->
   <session-config>
      <session-timeout>10</session-timeout>
   </session-config>
            <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
   </listener>
   <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>

   <!-- RestEasy -->
   <context-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/rest</param-value>
   </context-param>
   <context-param>
      <param-name>resteasy.injector.factory</param-name>
      <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
   </context-param>
   <servlet>
      <servlet-name>resteasy-servlet</servlet-name>
      <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
      </servlet-class>
      <init-param>
          <param-name>javax.ws.rs.Application</param-name>
          <param-value>org.jk.rest.RestEasyApplication</param-value>
      </init-param>
    </servlet>
          <servlet-mapping>
            <servlet-name>resteasy-servlet</servlet-name>
            <url-pattern>/rest/*</url-pattern>
          </servlet-mapping>

</web-app>


 

 

http://stackoverflow.com/questions/6345976/using-jax-rs-resteasy-with-cdi-weld-in-tomcat-7 

In this question there is an answer said this has been fixed. But i don't know why i can't inject in the resteasy bean.

 

 

Here is my pom.xml

 

<dependencies>
                    <dependency>
                              <groupId>org.jboss.weld.servlet</groupId>
                              <artifactId>weld-servlet</artifactId>
                              <version>1.1.5.Final</version>
                    </dependency>
                    <dependency>
                              <groupId>org.jboss.resteasy</groupId>
                              <artifactId>resteasy-jaxrs</artifactId>
                              <version>2.3.2.Final</version>
                              <exclusions>
                                        <exclusion>
                                                  <groupId>commons-httpclient</groupId>
                                                  <artifactId>commons-httpclient</artifactId>
                                        </exclusion>
                                        <exclusion>
                                                  <groupId>javax.servlet</groupId>
                                                  <artifactId>servlet-api</artifactId>
                                        </exclusion>
                                        <exclusion>
                                                  <groupId>javax.xml.bind</groupId>
                                                  <artifactId>jaxb-api</artifactId>
                                        </exclusion>
                                        <exclusion>
                                                  <groupId>com.sun.xml.bind</groupId>
                                                  <artifactId>jaxb-impl</artifactId>
                                        </exclusion>
                              </exclusions>
                    </dependency>
                    <dependency>
                              <groupId>org.codehaus.jettison</groupId>
                              <artifactId>jettison</artifactId>
                              <version>1.3.1</version>
                    </dependency>
                    <dependency>
                              <groupId>org.jboss.resteasy</groupId>
                              <artifactId>resteasy-jettison-provider</artifactId>
                              <version>2.3.2.Final</version>
                              <exclusions>
                                        <exclusion>
                                                  <groupId>javax.xml.bind</groupId>
                                                  <artifactId>jaxb-api</artifactId>
                                        </exclusion>
                                        <exclusion>
                                                  <groupId>com.sun.xml.bind</groupId>
                                                  <artifactId>jaxb-impl</artifactId>
                                        </exclusion>
                                        <exclusion>
                                                  <groupId>javax.xml.stream</groupId>
                                                  <artifactId>stax-api</artifactId>
                                        </exclusion>
                              </exclusions>
                    </dependency>
                    <dependency>
                              <groupId>org.jboss.resteasy</groupId>
                              <artifactId>resteasy-cdi</artifactId>
                              <version>2.3.2.Final</version>
                    </dependency>


                    <!-- log4j -->
                    <dependency>
                              <groupId>log4j</groupId>
                              <artifactId>log4j</artifactId>
                              <version>1.2.16</version>
                    </dependency>


                    <!-- JPA -->
                    <dependency>
                              <groupId>org.hibernate.javax.persistence</groupId>
                              <artifactId>hibernate-jpa-2.0-api</artifactId>
                              <version>1.0.1.Final</version>
                    </dependency>
                    <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-entitymanager</artifactId>
                              <version>4.1.2</version>
                              <exclusions>
                <exclusion>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
                    </dependency>
                    <!-- JSR-303 (Bean Validation) Implementation -->
                    <!-- Provides portable constraints such as @Email -->
                    <!-- Hibernate Validator is shipped in JBoss -->
                    <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-validator</artifactId>
                              <version>4.2.0.Final</version>
                              <scope>provided</scope>
                              <exclusions>
                                        <exclusion>
                                                  <groupId>org.slf4j</groupId>
                                                  <artifactId>slf4j-api</artifactId>
                                        </exclusion>
                              </exclusions>
                    </dependency>
                    <!-- Now we declare any tools needed -->


                    <!-- Annotation processor to generate the JPA 2.0 metamodel classes for 
                              typesafe criteria queries -->
                    <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-jpamodelgen</artifactId>
                              <version>1.1.1.Final</version>
                              <scope>provided</scope>
                    </dependency>
                    <dependency>
                              <groupId>mysql</groupId>
                              <artifactId>mysql-connector-java</artifactId>
                              <version>5.1.19</version>
                    </dependency>

          </dependencies>
          <distributionManagement>
                    <repository>
                              <id>jboss-releases-repository</id>
                              <name>JBoss Releases Repository</name>
                              <url>https://repository.jboss.org/nexus/service/local/staging/deploy/maven2/</url>
                    </repository>
          </distributionManagement>
          <build>
                    <plugins>
                              <plugin>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-deploy-plugin</artifactId>
                                        <configuration>
                                                  <skip>true</skip>
                                        </configuration>
                              </plugin>
                              <plugin>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-compiler-plugin</artifactId>
                                        <configuration>
                                                  <source>1.6</source>
                                                  <target>1.6</target>
                                        </configuration>
                              </plugin>
                    </plugins>
          </build>