我在 Grizzly 上运行了一些使用 Jersey 的RESTful 服务。所有带有@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();

最佳答案

经过大量调查,我找到了解决方案。我在这里添加它,因为有人可能会从中受益。
问题
我发现,在接口方法上添加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和路径都没有关系。但是这些必须添加到实现方法中。至少这对我有用,我不知道为什么界面中的注释不起作用。正如J2EE规范所说:

块引用
为了与其他Java EE规范保持一致,建议始终重复注释,而不要依赖注释继承。

因此,我在类中添加了注释。

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

10-10 21:48