当使用JAX RS Client API时,是否有办法通过拦截器向请求中添加标头,但不能通过显式设置标头:
Client client = ClientBuilder.newClient();
Response response = client.target("someUrl").path("somePath").request().get();
以AOP方式
答案 0 :(得分:5)
@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);