[英]How to intercept an AsyncClientHttpRequest using AsyncRestTemplate?
I'm developing an asynchronous REST client using the spring AsyncRestTemplate
helper class. 我正在使用spring
AsyncRestTemplate
帮助程序类开发异步REST客户端。
The client needs to send a token in the header of every requests. 客户端需要在每个请求的标头中发送令牌。
It is possible to add an interceptor when using the HttpAsyncClient
(of http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html ) as the underlying http client of the rest template : 使用
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);
However, if for some reason I need to change the underlying client, this interceptor can no longer be used. 但是,如果由于某种原因我需要更改底层客户端,则不能再使用此拦截器。
Is there any other way to intercept an AsyncClientHttpRequest
using an interceptor agnostic of the underlying http client ? 是否有任何其他方法来拦截
AsyncClientHttpRequest
使用基础http客户端的拦截器不可知?
No, not through the AsyncRestTemplate
and not through the HttpAsyncClient
. 不,不是通过
AsyncRestTemplate
而是通过HttpAsyncClient
。 Neither of those interfaces provide a mutator for adding HttpRequestInterceptor
instances. 这些接口都没有提供用于添加
HttpRequestInterceptor
实例的mutator。
As far as I know, only the builders for these are mutable. 据我所知,只有这些的建设者是可变的。 So even if you could get the request factory or the client, you'd still not be able to change them.
因此,即使您可以获得请求工厂或客户端,您仍然无法更改它们。
You have to intercept their actual creation, which, depending on your setup, may not be possible. 你必须拦截他们的实际创作,这取决于你的设置,可能是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.