java - 所有带@PathParam 的 Jersey 路线返回 404

标签 java rest jersey grizzly

我有几个使用 JerseyRESTful 服务,在 Grizzly 上运行。所有带@PathParam 的路由都会返回404 错误码。任何人都可以指导在哪里查看吗?

工作:

@GET
@Path("/testget")
@Produces(MediaType.APPLICATION_JSON)
Response testGet(){
    //working
}

不工作:

@GET
@Path("/testpath/{id}")
@Produces(MediaType.APPLICATION_JSON)
Response testPath(@PathParam("id") String id){
    //not working, return 404
}

如果我删除路径参数,它就会开始工作。但我需要路径参数。

灰熊代码:

        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.register(TestController.class);

        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URL), resourceConfig, false);
        server.start();

最佳答案

经过大量调查,我找到了解决方案。我将其添加到此处,因为有人可能会从中受益。

问题

我发现,在接口(interface)方法上添加 POST 和 Path 会导致问题。当方法参数中有 @PathParam 时,就会发生这种情况。

有问题: 界面:

@POST
@Path("/test/{id}")
public String testPost(@PathParam("id") String id);

类(基础资源在类级别路径注释):

@Override
public String testPost(@PathParam("id") String id){
    return "hello" + id;
}

解决方案

类:

@POST
@Path("/test/{id}")
@Override
public String testPost(@PathParam("id") String id){
    return "hello" + id;
}

我是否在界面上添加POST和Path并不重要。但是那些必须添加到实现方法中。至少这对我有用,我不知道为什么界面中的注释不起作用。正如 J2EE 规范所说:

Blockquote For consistency with other Java EE specifications, it is recommended to always repeat annotations instead of relying on annotation inheritance.

所以,我在类中添加注释。

关于java - 所有带@PathParam 的 Jersey 路线返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38807675/

相关文章:

java - 在 ExceptionMapper 中注入(inject)单例

java - 从 tcpflow 输出格式化 IP?

java - 在 Zxing 中启用 PDF417 解码

java - Spring Rest API 最大大小 HTTP POST

Jersey JAX-RS : extend logging over resource-class dispatching

java - Java EE 新手,使用 Jersey SDK 获取 HTTP 404 的 Json web 服务

Java Swing GUI,自定义JtabbedPane风格

java - 当我的 Java 小程序在较旧的 Java 版本上运行但使用新类时会发生什么情况?

REST API - 设计考虑

java - 带有 REST API 的 Spring Security