JAX-RS Client Filter to Modify Header Before Request is Dispatched to server

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:28:52

问题


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:


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() 
    }
}  


来源:https://stackoverflow.com/questions/37429405/jax-rs-client-filter-to-modify-header-before-request-is-dispatched-to-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!