覆盖泽西岛的@Path
我一直在尝试制作一个 Jersey 应用程序,该应用程序从路径中采用以下结构:
示例 1(由于 stackoverflow 限制,http:// 被省略) example.com/source/{source-id}/ example.com/source/
使用代码(错误处理和省略不需要的代码):
使用代码(总结):
@Path("/source/")
public class SourceREST {
...
@GET
public Response getSource() {
return Response.ok("Sources List (no field)").build();
}
@GET
@Path("/{source-id}")
public Response getSource(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID).build();
}
}
工作正常。
示例2: example.com/data/{source-id}/{info-id}/ example.com/data/{source-id}/
使用代码(错误处理和省略不需要的代码):
@Path("/data/")
public class DataREST {
...
@GET
@Path("/{source-id}")
public Response getContext(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID + " Last ContextInfo").build();
}
@GET
@Path("/{source-id}/{data-id}")
public Response getContext(@PathParam("source-id") String sourceID,
@PathParam("data-id") String dataID) {
return Response.ok("Source: " + sourceID + " Data: " + dataID).build();
}
}
在示例 2 中,我可以访问像 example.com/data/111/222/ 这样的 URL,但试图获取 hexample .com/data/111/ 给我一个 405 错误代码(不允许的方法)。我还尝试创建一个完整的方法来检查 {data-id} 是否为空或空白,并在这种情况下像第一种方法一样处理请愿书,但也不起作用。
有什么想法吗?
谢谢!
I've been trying to make a Jersey app that takes the following structure from the path:
Example 1 (http:// omitted due to stackoverflow restrictions)
example.com/source/{source-id}/
example.com/source/
With code (error handling and unneeded code omitted):
With code (sum up):
@Path("/source/")
public class SourceREST {
...
@GET
public Response getSource() {
return Response.ok("Sources List (no field)").build();
}
@GET
@Path("/{source-id}")
public Response getSource(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID).build();
}
}
It works fine.
Example 2:
example.com/data/{source-id}/{info-id}/
example.com/data/{source-id}/
With code (error handling and unneeded code omitted):
@Path("/data/")
public class DataREST {
...
@GET
@Path("/{source-id}")
public Response getContext(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID + " Last ContextInfo").build();
}
@GET
@Path("/{source-id}/{data-id}")
public Response getContext(@PathParam("source-id") String sourceID,
@PathParam("data-id") String dataID) {
return Response.ok("Source: " + sourceID + " Data: " + dataID).build();
}
}
In example 2, I can access an URL like example.com/data/111/222/ fine, but trying to get hexample.com/data/111/ gives me a 405 Error Code (Method Not Allowed). I've tried also to make a whole method that checks if {data-id} is empty or blank and in that case process the petition as in the first method, but doesn't work either.
Any idea?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚将您的示例 2 粘贴到资源中,它似乎工作正常。我正在使用泽西岛 1.1.5。
以下是我的测试结果。
http://localhost:8090/MyHealth/helloworld/111
http://localhost:8090/MyHealth/helloworld/111/
http://localhost:8090/MyHealth/helloworld/111/222
http://localhost:8090/MyHealth/helloworld/111/222/
I just pasted your example 2 into a Resource and it seems to work fine. I am using Jersey 1.1.5.
Below are the results from my tests.
http://localhost:8090/MyHealth/helloworld/111
http://localhost:8090/MyHealth/helloworld/111/
http://localhost:8090/MyHealth/helloworld/111/222
http://localhost:8090/MyHealth/helloworld/111/222/
您可以尝试使用根资源的路径,例如
@Path("/data/{source-id}")
。那么第一个方法将仅具有@GET
注释,而第二个方法将具有@GET @Path("{data-id}")
。更重要的是,我建议使用不同的方法名称(虽然它们可能执行不同的操作),但我不确定这是导致您的问题的原因。
You can try using path for root resource like
@Path("/data/{source-id}")
. Then the first method would only have@GET
annotation while the second one would have@GET @Path("{data-id}")
.What is more, I would suggest using different method names (while they probably performs different actions), but I'm not sure that it is the cause of your problem.