我正在使用spring AsyncRestTemplate助手类开发一个异步REST客户端。
客户端需要在每个请求的头部中发送一个令牌。
当使用(http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html的) HttpAsyncClient作为rest模板的底层http客户端时,可以添加拦截器:
HttpRequestInterceptor interceptor = (request, context) -> request.addHeader("token", "value");
CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.addInterceptorLast(interceptor)
.build();
HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client);
AsyncRestTemplate template = new AsyncRestTemplate(factory);但是,如果出于某种原因我需要更改底层客户端,则不能再使用此拦截器。
是否有其他方法可以使用与底层AsyncClientHttpRequest客户端无关的拦截器来拦截http?
发布于 2014-10-03 22:51:40
不,不是通过AsyncRestTemplate也不是通过HttpAsyncClient。这两个接口都不提供用于添加HttpRequestInterceptor实例的赋值函数。
据我所知,只有这些的构建器是可变的。因此,即使您可以获得请求工厂或客户端,您仍然无法更改它们。
您必须截获它们的实际创建,这取决于您的设置,这可能是不可能的。
https://stackoverflow.com/questions/26176685
复制相似问题