【问题标题】:Spring Jersey and Spring boot togetherSpring Jersey 和 Spring Boot 在一起
【发布时间】:2021-10-17 17:02:29
【问题描述】:

我最近了解到 JAX-RS 和 jersey 是它的实现之一。到目前为止,我一直在使用 Spring boot 来使用 rest api。

我只是在尝试将两个依赖项放在一起。所以我的 pom 看起来像:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

现在我添加了一个具有如下端点的控制器:

@Service
@Path("/")
@RestController
public class TestController {

    @GET
    @Path("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/test2")
    public String test2() {
        return "Hello World - spring";
    }
}

现在,如果我尝试像这样热这些端点:

http://localhost:8080/test

http://localhost:8080/test2

我只收到 jersey 的回复(“Hello World”)。

任何人都可以帮助我了解它如何注册这些端点以及为什么 @GetMapping 端点没有被识别。

谢谢。

【问题讨论】:

    标签: spring spring-boot jersey


    【解决方案1】:

    您不应将 JAX-RS(@PATH.@GET 等)和 Spring MVC(@RestController@GetMapping 等)混合在同一个类中,或者甚至,理想情况下,在同一个应用程序中。从纯 Spring 的角度来看,@Service@RestController 也不应该一起使用。

    如果你真的想混合 Jersey 和 Spring MVC,你应该在单独的类中这样做。您还需要 configure Jersey to run as a filter and to forward requests that it cannot handle 以便 Spring MVC 有机会这样做。

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2017-10-02
      • 2015-01-12
      • 2018-11-08
      • 2017-02-19
      • 1970-01-01
      相关资源
      最近更新 更多