JAX RS 客户端 API 拦截器

JAX RS Client API interceptor

有没有办法通过拦截器将 header 添加到请求中,而不是通过显式设置 header,当使用 JAX RS 客户端 API 时:

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

AOP方式

创建 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);