【问题标题】:RESTEasy + Spring 3 + MavenRESTEasy + Spring 3 + Maven
【发布时间】:2014-05-21 20:03:30
【问题描述】:

我在尝试将应用配置为使用 RESTEasy + Spring 3 + Maven 时遇到了一些问题。到目前为止,这就是我所拥有的:

服务接口:

package com.test.service;
public interface ProcessRequestService {
    String test();
}

服务实现:

package com.test.service;
import org.springframework.stereotype.Service;

@Service
public class ProcessRequestServiceImpl implements ProcessRequestService {
    @Override
    public String test() {
        return "Resteasy Test";
    }
}

组件:

package com.test.web;

@Component
@Path("/message")
public class Main {

    @Autowired
    private ProcessRequestService processRequestService;

    public void setService(ProcessRequestService processRequestService) {
        this.processRequestService = processRequestService;
    }

    @GET
    @Path("/example")
    public Response example() {
        String result = processRequestService.test();
        return Response.status(200).entity(result).build();
    }
}

我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>3.2.8.RELEASE</spring.version>
        <resteasy.version>3.0.4.Final</resteasy.version>
    </properties>

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
        </repository>
    </repositories>

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

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

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

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>client</finalName>
    </build>

</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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" metadata-complete="true">

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

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/api</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>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

</web-app>

最后是 mvc-dispatcher.servlet.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.test" />
    <context:annotation-config />

</beans>

我要避免的是使用@Service 注释在 xml 文件中声明许多 bean。

当我将此应用程序部署到 jboss 6.1 时,我得到一个 NullPointerException。我猜这是因为 Spring 无法将服务自动连接到组件,但我不确定为什么会发生这种情况。

我可以通过对this 教程进行一些修改来实现这一点,但它使用的是 Spring 的 RequestMapping 而不是 RESTEasy。

我们将不胜感激。

【问题讨论】:

  • 您可以使用支持 Spring 的 JAX-RS 的 Apache CXF 实现。可能不会有任何代码更改。

标签: java spring maven service resteasy


【解决方案1】:

我有同样的问题。请试试这个。

在 POM 文件中添加以下依赖项:

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.4.Final</version>
</dependency>

并在 mvc-dispatcher.servlet.xml 中在 component-scan 上面添加 bean 定义

<bean class="org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware"/>

这将解决空指针异常。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我要避免的是在 xml 文件中声明许多 bean y 使用@Service 注解

    你正在做的很好。从注释而不是 XML 进行映射并不是为您提供NullPointerException

    当我将此应用程序部署到 jboss 6.1 时,我得到了一个 空指针异常。我想这是因为 Spring 无法 将服务自动连接到组件,但我不确定这是为什么 正在发生

    是的,JBoss 无法注入,因为 JAX-RS(在您的情况下为 RESTEasy)不知道 Spring bean。它们不是开箱即用的。

    发生这种情况是因为 JAX-RS 实现不会从 @Autowired 中查看其资源(此上下文中的资源是 @Path 带注释的类)。

    在 Java EE 容器中使用 Spring Beans 需要额外的工作。

    我们将不胜感激

    我能找到的所有选项都是:

    顺便说一句,我没有测试这些示例。另一个选项是:

    • 如您所说,使用 Spring MVC 即可。
    • 使用 EJB 而不是 Spring Beans,这很有意义,因为您在 Java EE 服务器下。
    • 也许试试泽西岛。

    请记住,Spring 的事务独立于 Java EE 事务。您正在将 JAX-RS 与 Spring 混合使用。交易需要额外的爱。

    如果我是你,我会考虑使用 EJB 而不是 Spring Beans。

    我在尝试将应用配置为使用 RESTEasy + 时遇到一些问题 春季 3 + Maven

    查看我关于此主题的另一个答案。也许它会帮助您确定哪种 RESTful 引擎更适合您的解决方案。

    Spring annotations for GET requests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2011-07-26
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多