JAX RS Client API拦截器

时间:2015-10-08 15:26:04

标签: java jax-rs aop interceptor

当使用JAX RS Client API时,是否有办法通过拦截器向请求中添加标头,但不能通过显式设置标头:

Client client = ClientBuilder.newClient();
Response response = client.target("someUrl").path("somePath").request().get();

以AOP方式

1 个答案:

答案 0 :(得分:5)

创建ClientRequestFilter

@Provider
public class MyClientRequestFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add("Authorization", "value");
    }
}

并在Client

中注册
Client client = ClientBuilder.newClient().register(MyClientRequestFilter.class);