Java 所有带有@PathParam的jersey路线返回404

Java 所有带有@PathParam的jersey路线返回404,java,rest,jersey,grizzly,Java,Rest,Jersey,Grizzly,我有几个RESTful服务使用Jersey,运行在Grizzly上。带有@PathParam的所有路由返回404错误代码。有人能告诉我去哪里调查吗 工作: @GET @Path("/testget") @Produces(MediaType.APPLICATION_JSON) Response testGet(){ //working } 不工作: @GET @Path("/testpath/{id}") @Produces(MediaType.APPLICATION_JSON) Re

我有几个RESTful服务使用Jersey,运行在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();

经过大量调查,我找到了解决办法。我在这里加上它,因为有人可能会从中受益

问题

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

问题: 接口:

@POST
@Path("/test/{id}")
public String testPost(@PathParam("id") String id);
类(基本资源位于类级别的路径注释上):

解决方案

类别:

@POST
@Path("/test/{id}")
@Override
public String testPost(@PathParam("id") String id){
    return "hello" + id;
}
我是否在接口上添加帖子和路径并不重要。但这些必须添加到实现方法中。至少这对我是有效的,我不知道为什么接口中的注释不起作用。正如J2EE规范所说:

大宗报价 为了与其他JavaEE规范保持一致,建议始终重复注释,而不是依赖注释继承


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

你是说url
../testget
可以工作,但是像
../testpath/abc
这样的url不工作吗?没错,这就是问题所在。我猜jersey/grizzly无法将testpath/abc映射到testpath/{id}无法复制。我刚刚使用中的说明创建了一个全新的项目,然后将这两个方法添加到示例
MyResource
类中,使它们返回“Hello from”。两者都很好。您应该从实现中删除@PathParam,否则接口上的所有注释都将被忽略。这个答案更详细地解释了这一点。
@POST
@Path("/test/{id}")
@Override
public String testPost(@PathParam("id") String id){
    return "hello" + id;
}