Search code examples
javaspringresteasyautowiredcomponent-scan

@Autowired not working with resteasy


I know this has been asked before, but I've checked this code against all the answers provided and still cannot see what is wrong with it... My @Autowired fields are simply not being injected (ie. they are null), here is my setup:

  • Tomcat 7
  • RestEasy 3.0.8
  • Spring framework 4.0.6
  • Java 8

pom.xml (relevant dependencies)

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jettison-provider</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-spring</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

web.xml

<!--  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param> -->

<context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>com.reporting.rest.MyRestResource</param-value>
</context-param>

<!-- this need same with resteasy servlet url-pattern -->
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/reporting</param-value>
</context-param>

<!--  Must come before any other listener -->
<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/reporting/*</url-pattern>
</servlet-mapping>  

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
    </listener-class>
</listener>    

application-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.reporting" />

</beans>

And finally my Java class:

package com.reporting.rest;

@Path("/events")
@Produces("application/json")
public class MyRestResource {
....
@Autowired IProductionEventService productionEventService;
....
}

I have tried to annotate the class itself with @Component as well, but it did not help.

Thanks in advance!


Solution

  • Finally found the problem, it looks like there is an issue integrating resteasy to spring 4 - https://issues.jboss.org/browse/RESTEASY-1012

    I downgraded the spring version and it worked. Hope this helps someone else save time!