Java 如何从ContainerRequestContext获取路径参数
我想为我的RESTAPI端点应用授权过滤器,过滤器需要path参数来进行过滤。以下是我的端点和代码: 终点:Java 如何从ContainerRequestContext获取路径参数,java,jersey,jax-rs,Java,Jersey,Jax Rs,我想为我的RESTAPI端点应用授权过滤器,过滤器需要path参数来进行过滤。以下是我的端点和代码: 终点: curl --url 'localhost:80/reports/resources/org/12345/product/111 ' --request GET --header 'Authorization: <token here>' 过滤器: @PreMatching @RoleAuthenticated public class AuthorizationFilte
curl --url 'localhost:80/reports/resources/org/12345/product/111 ' --request GET --header 'Authorization: <token here>'
过滤器:
@PreMatching
@RoleAuthenticated
public class AuthorizationFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MultivaluedMap<String, String> pathparam = requestContext.getUriInfo().getPathParameters(); <-- return empty map
}
它怎么会返回一张空地图?如何从
ContainerRequestContext
获取路径参数 您在过滤器上使用了一个@PreMatching
注释
Javadoc说:
如果过滤器应用于预匹配扩展点,
i、 e.在JAX-RS运行时执行任何请求匹配之前,
过滤器必须使用@prematch注释进行注释
因此,在传入请求被JAX-RS运行时匹配到特定资源之前调用过滤器,因此路径参数为空
所以我想你需要删除
@PreMatching
注释。你说得对!在我删除@prematch之后,它就可以工作了
@PreMatching
@RoleAuthenticated
public class AuthorizationFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MultivaluedMap<String, String> pathparam = requestContext.getUriInfo().getPathParameters(); <-- return empty map
}
orgId 12345
productId 111