首页 > 解决方案 > 如何在 Jersey 2 的 postfilter 中修改查询参数

问题描述

我正在尝试修改后匹配过滤器中的查询参数。我不能在预匹配过滤器中执行此操作,因为过滤器逻辑取决于资源类和方法注释。我正在使用 Jersey 2.x 和 Spring Boot。

我无法通过调用来修改查询参数,setRequestUri因为它仅在预匹配过滤器(参考)中允许。

我不想设置属性,因为这需要更改所有现有服务以从属性中读取。如果没有其他选项可用,我可能需要使用属性方法。

通过调用注册过滤器ResourceConfig#register(cls, priority)

过滤器类实现ContainerRequestFilter.

public class MyFilter implements ContainerRequestFilter {
    @Override
    public void filter(final ContainerRequestContext requestContext) {
        // modifiedUri is the uri with modified query params
        // requestContext.setRequestUri(modifiedUri) ==> Throws IllegalStateException - "Method could be called only in pre-matching request filter."
    }
}

我尝试在线搜索并找到设置请求属性的解决方案。我找到了另一种建议创建包装器( )的解决方案HttpServletRequestWrapper。但是在上面的过滤器中,我无法访问filterChain. 由于过滤器需要访问资源类和资源方法注解,我相信我需要使用 Jersey 过滤器并且不能使用 Servlet 过滤器(参考)。

标签: javajerseyjersey-2.0servlet-filtersquery-parameters

解决方案


推荐阅读