gpt4 book ai didi

java - RESTEasy + Spring 3 + Maven

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:24 26 4
gpt4 key购买 nike

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

服务接口(interface):

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。

任何帮助将不胜感激。

最佳答案

我也遇到了同样的问题。请尝试一下。

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

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

并在 mvc-dispatcher.servlet.xml 中添加组件扫描上方的 bean 定义

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

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

关于java - RESTEasy + Spring 3 + Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23792749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com