spring - Autowired 不适用于 RESTEasy 和 Spring 3

标签 spring jax-rs resteasy autowired

我在将@Autowired 与 RESTEasy 2.2.1.GA 和 Spring 3.0.5.RELEASE 一起使用时遇到问题。我总是在

String result = customerBo.getMsg();

进入我的服务。

如何纠正我的代码的 Autowiring 行为?

这是我的 RESTEasy 服务:

@Component
@Path("/api")
public class Rest {

    @Autowired
    CustomerBo customerBo;

      @GET
      @Produces("text/plain")
      public String getXMLMessage() {
              String result = customerBo.getMsg();
              return result;
      }
}

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- JBoss RESTEasy -->
    <context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>ua.softserve.hotel.api.Rest</param-value>
    </context-param>

    <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>/api</url-pattern>
    </servlet-mapping>
    <!-- JBoss RESTEasy end -->

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>charsetFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>charsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

这是我的 root-context.xml:

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Root Context:   -->

    <context:annotation-config />

    <bean id="customerBo" class="ua.softserve.hotel.api.CustomerBoImpl">
    </bean>

    <context:component-scan base-package="ua.softserve.hotel" />

    <import resource="data.xml" />
    <import resource="security.xml" />

</beans>

最佳答案

我的问题解决了。 问题在于,对于实现,我使用了来自不同来源的文章,用于不同版本的库。

我在官方文档中找到的问题的正确答案: http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/RESTEasy_Spring_Integration.html

关于spring - Autowired 不适用于 RESTEasy 和 Spring 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820476/

相关文章:

java - 尽管使用 JsonIgnore,但未能延迟初始化 ManyToMany 关系中的角色集合

java - 自定义 StoredProcedure 类作为 Spring 组件并扩展 jdbc StoredProcedure

java - 在 Tomcat 中将配置与 WAR 分离的优雅方法

java - 为 HTTP 路径映射的处理程序方法不明确 同一 URI GET 和 POST 发生错误

java - RESTEasy - @Path 需要完整路径吗?

java - 如何创建 MessageBodyWriter 以在 RestEasy 中将自定义对象作为 HTML 返回?

spring - 无法读取 TLD "META-INF/c.tld"

java - 如何在我的 Web 服务中创建自定义 JSON/XML 响应

rest - 使用 Jersey (JAX-RS) 呈现 HTML

spring - 将 spring-boot 与 RESTEasy 集成