【发布时间】: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