如何从javax.rs.ws.core.UriInfo以编程方式获取矩阵参数



有什么方法可以从javax.rs.ws.core.UriInfo以编程方式获取矩阵参数吗?我知道您可以通过简单地执行uriInfo.getQueryParameters()来获得作为MultivaluedMap的查询参数。不支持矩阵参数吗?或者可以通过调用uriInfo.getPathParameters()获得它们?

/../之间的路径的每一部分都是PathSegment。由于每个段都允许使用矩阵参数,因此能够通过PathSegment访问它们是有意义的。输入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) {}

对于路径模板regex允许多段匹配的情况,也可以注入List<PathSegment>。例如

@Path("{segment: .*}/hello/world")
public Response doSomething(@PathParam("segment") List<PathSegment> segments) {}

如果URI为/stack/overflow/hello/world,则段stackoverflow将被放入列表中。

此外,我们可以简单地使用@MatrixParam来注入矩阵参数的值,而不是注入PathSegment

@Path("/hello")
public Response doSomething(@MatrixParam("world") String world) {}
// .../hello;world=Hola!

最新更新