JAX-RS Client Filter to Modify Header Before Reque

2019-09-10 21:35发布

In JAX-RS (RestEasy), I want to implement a client filter that modifies the header before sending the request so I don't do this manually for every single call.

Currently I'm doing this in the receiving end to intercept requests before arriving to the resource.

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        // read header
    }

Now I know this (Correct me if I'm wrong):

In the receiving end, ContainerRequestFilter can be used before the request arrives to the resource and get the request.

But I want to implement this in the client side, to modify the header before the request is ever sent to the server. Can the same server filter be used or there is something similar to for the client?

1条回答
Ridiculous、
2楼-- · 2019-09-10 21:57

You must register a ClientRequestFilter into your Client

Client client = ClientBuilder.newClient().register(MyFilter.class);

@Provider
public class MyFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext ctx) throws IOException {
        // modify header before send: ctx.getHeaders() 
    }
}  
查看更多
登录 后发表回答