2

我知道之前有人问过这个问题,但是我已经根据提供的所有答案检查了这段代码,但仍然看不出它有什么问题......我的@Autowired 字段根本没有被注入(即它们为空),在这里是我的设置:

  • 雄猫 7
  • 轻松3.0.8
  • Spring框架4.0.6
  • 爪哇 8

pom.xml(相关依赖)

    <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>    

应用程序上下文.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>

最后是我的 Java 类:

package com.reporting.rest;

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

我也尝试用 @Component 注释类本身,但它没有帮助。

提前致谢!

4

2 回答 2

4

您不能直接将 autowire 与 resteasy 资源一起使用。你应该告诉 spring,特定的类将使用自动装配。文档可以在这里找到。

您可以使用以下代码执行此操作。

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

}

你应该有以下依赖关系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>  
于 2014-09-05T06:21:18.230 回答
1

终于找到问题了,看来resteasy到spring 4的集成有问题 - https://issues.jboss.org/browse/RESTEASY-1012

我降级了春季版本,它工作。希望这可以帮助其他人节省时间!

于 2014-09-05T06:35:08.237 回答