Search code examples
javajakarta-eejax-rsresteasyjava-ee-7

Attaching query parameters in a ClientRequestFilter


I simply need to attach query parameters onto an outgoing request. (Java EE 7.0, JAX-RS 2.0)

In specifics, I currently using the RESTeasy Client ver 3.0.14, so I make my calls using the fancy interface-proxy system. I was attempting to produce something like this:

myapplication/api/path?timestamp=000

with:

@Provider
public class MyRequestFilter implements ClientRequestFilter {

    @Context
    private HttpServletRequest servletRequest;

    public void filter(ClientRequestContext requestContext) throws IOException {

        servletRequest.getParameterMap().put("timestamp", new String[]{
                String.valueOf(new Date().getTime())
        });

    }
}

I made sure I was registering it with client.register(MyRequestFilter.class) as well. Feel free to ask questions. Thanks!


Solution

  • Credit to @peeskillet --

    Rebuild the URI from the requestContext like this:

    requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("key", value).build());
    

    You can now see the new query parameter with

    requestContext.getUri().toString();
    

    Again, verify that you register it when making the REST Client

    client.register(MyRequestFilter.class);