【发布时间】:2023-03-28 19:15:02
【问题描述】:
我一直在尝试制作一个 Jersey 应用,它从路径中获取以下结构:
示例 1(http:// 由于 stackoverflow 限制而省略) 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} 是否为空或空白,在这种情况下像第一种方法一样处理请愿书,但也不起作用。
有什么想法吗?
谢谢!
【问题讨论】:
-
你试过example.com/data/111(不带斜线)吗?
-
感谢您的提问,正是我要找的东西