我正在试图弄清楚如何设置类似于此处所解释的HTTP标头:
但是,我想使用RESTeasy 3.0功能(ResteasyClientBuilder和ResteasyWebtarget)而不是已弃用的ProxyFactory,如下所述:
只是为了澄清,我也不想在每个请求上设置标题/不希望它们传递给客户端,我希望它们设置在ResteasyClientBuilder / ResteasyWebtarget级别上可能的。
答案 0 :(得分:3)
找到解决方案。
诀窍是使用ResteasyClient注册ClientRequestFilter(下面方法的第2行):
public Resource getResource(Credentials credentials) {
ResteasyClient client = new ResteasyClientBuilder().build();
client.register(new AuthHeadersRequestFilter(credentials));
return client.target(restServiceRoot).proxy(Resource.class);
}
然后让您的请求过滤器执行以下操作:
public class AuthHeadersRequestFilter implements ClientRequestFilter {
private final String authToken;
public AuthHeadersRequestFilter(Credentials credentials) {
authToken = credentials.getAuthorizationHeader();
}
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().add("Authorization", authToken);
}
}