我有几个使用 Jersey 的 RESTful 服务,在 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/