我正在使用
spring AsyncRestTemplate帮助程序类开发异步REST客户端.
客户端需要在每个请求的标头中发送令牌.
使用HttpAsyncClient(http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html)作为其余模板的基础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);
但是,如果由于某种原因我需要更改底层客户端,则不能再使用此拦截器.