如何以编程方式从 javax.rs.ws.core.UriInfo 获取矩阵参数
How to get matrix parameters programmatically from javax.rs.ws.core.UriInfo
有什么方法可以从 javax.rs.ws.core.UriInfo
中以编程方式获取矩阵参数吗?我知道您只需执行 uriInfo.getQueryParameters()
即可将查询参数作为 MultivaluedMap
获取。不支持矩阵参数吗?或者可以通过调用 uriInfo.getPathParameters()
?
获得
/../
之间路径的每个部分都是 PathSegment
. And since matrix parameters are allowed for each segment, it makes sense to be able to access them via the PathSegment
. Enter PathSegment#getMatrixParameters()
:-)。所以..
List<PathSegment> pathSegments = uriInfo.getPathSegments();
for (PathSegment segment: pathSegments) {
MultivaluedMap<String, String> map = segment.getMatrixParameters();
...
}
也可以注入 PathSegment
@Path("/{segment}")
public Response doSomething(@PathParam("segment") PathSegment segment) {}
对于路径模板正则表达式允许多段匹配的情况,您还可以注入 List<PathSegment>
。例如
@Path("{segment: .*}/hello/world")
public Response doSomething(@PathParam("segment") List<PathSegment> segments) {}
如果 URI 为 /stack/overflow/hello/world
,则段 stack
和 overflow
将被放入列表。
此外,我们可以简单地使用@MatrixParam
来注入矩阵参数
的值,而不是注入PathSegment
@Path("/hello")
public Response doSomething(@MatrixParam("world") String world) {}
// .../hello;world=Hola!
有什么方法可以从 javax.rs.ws.core.UriInfo
中以编程方式获取矩阵参数吗?我知道您只需执行 uriInfo.getQueryParameters()
即可将查询参数作为 MultivaluedMap
获取。不支持矩阵参数吗?或者可以通过调用 uriInfo.getPathParameters()
?
/../
之间路径的每个部分都是 PathSegment
. And since matrix parameters are allowed for each segment, it makes sense to be able to access them via the PathSegment
. Enter PathSegment#getMatrixParameters()
:-)。所以..
List<PathSegment> pathSegments = uriInfo.getPathSegments();
for (PathSegment segment: pathSegments) {
MultivaluedMap<String, String> map = segment.getMatrixParameters();
...
}
也可以注入 PathSegment
@Path("/{segment}")
public Response doSomething(@PathParam("segment") PathSegment segment) {}
对于路径模板正则表达式允许多段匹配的情况,您还可以注入 List<PathSegment>
。例如
@Path("{segment: .*}/hello/world")
public Response doSomething(@PathParam("segment") List<PathSegment> segments) {}
如果 URI 为 /stack/overflow/hello/world
,则段 stack
和 overflow
将被放入列表。
此外,我们可以简单地使用@MatrixParam
来注入矩阵参数
PathSegment
@Path("/hello")
public Response doSomething(@MatrixParam("world") String world) {}
// .../hello;world=Hola!