Jersey 没有注册我的资源

Paw*_*ski 2 java jax-rs jersey-2.0

我有一个使用 Spring Boot(2.0.0) 和 Jersey 的非常基本的应用程序。不幸的是,当我尝试调用暴露的端点时,我收到错误 404。

我的代码如下所示:

主要类:

public class App {
    public static void main( String[] args )  {
        SpringApplication.run(App.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

泽西配置:

@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig{
    public JerseyConfig() {
        register(FileResource.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

资源:

@Component
@Consumes(MediaType.APPLICATION_JSON)
public class FileResource {

    @POST
    @Path("/uploadfile")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void upload(@FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition disposition) {
        System.out.println(disposition.getName());

    }

    @GET
    @Path("/foo")
    public String foo() {
        return "foo";
    }
}
Run Code Online (Sandbox Code Playgroud)

Maven 依赖项:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
  <groupId>com.sun.jersey.contribs</groupId>
  <artifactId>jersey-multipart</artifactId>
  <version>1.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

当我打电话时:

GET /api/foo HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)

我得到:

{
    "timestamp": "2018-03-27T08:37:13.926+0000",
    "status": 404,
    "error": "Not Found",
    "message": "Not Found",
    "path": "/api/foo"
}
Run Code Online (Sandbox Code Playgroud)

第二个端点也是如此。在应用程序日志中,我可以找到Servlet .....JerseyConfig mapped to [/api/*] 有人知道它有什么问题吗?

Paw*_*ski 5

我发现出了什么问题。

事实证明,在 Jersey 中,每个资源都必须@Path在类级别上进行注释。将我的资源更改为:

@Component
@Consumes(MediaType.APPLICATION_JSON)
@Path("/uploadfile")   // THIS IS MANDATORY ANNOTATION
public class FileResource {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void upload(@FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition disposition) {
        System.out.println(disposition.getName());

    }

    @GET
    @Path("/foo")
    public String foo() {
        return "foo";
    }
}
Run Code Online (Sandbox Code Playgroud)

它开始工作。