2

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!

dhalia
  • 401
  • 5
  • 21
  • 3
    How are you integrating both frameworks? They won't get magically tied up. – Luiggi Mendoza Sep 05 '14 at 00:40
  • @Luiggi - from examples I have seen, this dependency should do: org.jboss.resteasy resteasy-spring 3.0.8.Final – dhalia Sep 05 '14 at 05:31
  • That's just copying the jars into your project library. It's like putting raw tomato and an entire lettuce in a plate and call it a salad... Please follow this tutorial: http://www.mkyong.com/webservices/jax-rs/resteasy-spring-integration-example/ – Luiggi Mendoza Sep 05 '14 at 05:36
  • @chrylis This is not a duplicate of the question you mention. I am not using new anywhere in the code. Thanks. – dhalia Sep 05 '14 at 05:36
  • @Luiggi Funny, but I don't think you fully read my question. I am using annotation configuration with context:component-scan to find my annotated classes, while the example you tell me to follow declares the beans in xml. The example also just shows how to use the ApplicationContext to look at container context and get the bean, which is nice (and in fact I am doing it), but not necessary for my problem. And finally, the pom dependencies and web.xml are the same (other than the versions). – dhalia Sep 05 '14 at 05:49
  • I read the content of your question and you haven't posted any code related to mix RestEasy with Spring, that's why I asked you in my comment if you were integrating both frameworks. Seems like you didn't or you're doing it wrong, and that's the specific part that you should post here rather than your pom or your Spring configuration (which are helpful but don't provide exact info about the main problem). – Luiggi Mendoza Sep 05 '14 at 05:51
  • Check out https://issues.jboss.org/browse/RESTEASY-1012 – geoand Sep 05 '14 at 06:21
  • It's a duplicate under SO standards because the underlying cause and solution are identical. The instance of your class where you're get the exception isn't a Spring-managed bean. – chrylis -cautiouslyoptimistic- Sep 05 '14 at 06:39
  • @geoand yes, that was the problem... I just came across the same link you mention as well. Downgrading the spring version worked. – dhalia Sep 05 '14 at 06:42

2 Answers2

4

You can't use autowire with resteasy resources directly. You should tell spring that, particular class is going to use autowiring. Docs can be found here.

You can do that using following code.

@Component
@Path("/trn")
public class TrnResource extends SpringBeanAutowiringSupport {

}

You should have following dependencies i you pom.xml,

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>     

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-framework.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>  
Maleen Abewardana
  • 13,600
  • 4
  • 36
  • 39
  • I'm sorry, but that is not true. Resteasy knows to look for annotated resources in a couple of ways - either through the resteasy.scan property or by listing out your resources directly in your web.xml. – dhalia Sep 05 '14 at 06:40
  • @dhalia - Yes, resteasy know, how to find there annotations, not spring annotations. BTW this is the easiest way, if you want to autowire in the resteasy resource. – Maleen Abewardana Sep 05 '14 at 06:49
1

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!

dhalia
  • 401
  • 5
  • 21
  • you did save me some time thanks. Some extra info: I've downgraded to the latest 3.x version. In my case, it only worked when I've annotated the rest service class with `@Component` and `@Path`. – felipe_gdr Dec 18 '14 at 20:19