I am trying to modify the query param in a post-matching filter. I cannot do this in pre-matching filter as the filter logic depends on the resource class and method annotations. I am using Jersey 2.x and Spring Boot.
I cannot modify the query param by calling setRequestUri
as it is allowed only in pre-matching filter (reference).
I do not want to set an attribute, as that would require changing all existing services to read from the attribute. If no other option is available, I might need to go with the attribute approach.
Filter is registered by calling ResourceConfig#register(cls, priority)
The filter class implements 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."
}
}
I tried searching online and found solutions that sets request attributes.
I found another solution which suggests creating a wrapper (HttpServletRequestWrapper
). But in the above filter, I do not have access to the filterChain
. Since the filter requires access to the resource class and resource method annotations, I believe I need to use a Jersey filter and cannot use the Servlet filter (Reference).