简体   繁体   English

如何使用AsyncRestTemplate拦截AsyncClientHttpRequest?

[英]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 : 使用HttpAsyncClienthttp://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.

相关问题 如何使用Netty4ClientHttpRequestFactory为Spring AsyncRestTemplate设置代理? - How to set proxy for Spring AsyncRestTemplate using Netty4ClientHttpRequestFactory? 使用WireMock和AsyncRestTemplate,如何测试故障? - Using WireMock and AsyncRestTemplate, how do I test a failure? 如何将Spring Retry与AsyncRestTemplate集成 - How to integrate Spring Retry with AsyncRestTemplate 如何增加AsyncRestTemplate类的超时时间? - How to increase timeout AsyncRestTemplate class? 如何使用AsyncRestTemplate同时进行多个调用? - How to use AsyncRestTemplate to make multiple calls simultaneously? 如果占用太多时间,如何取消AsyncRestTemplate HTTP请求? - How to cancel AsyncRestTemplate HTTP request if they are taking too much time? 在使用@Async批注的方法中使用Spring4 AsyncRestTemplate - Using Spring4 AsyncRestTemplate in a method which uses an @Async annotation 如何在拦截中更改响应主体(使用改造 2) - How to change response body in intercept (Using retrofit 2) java.io.IOException:在Weblogic服务器上使用AsyncRestTemplate时,连接被对等方重置 - java.io.IOException: Connection reset by peer' when using AsyncRestTemplate on weblogic server 如何使用spring AOP切入点表达式截取返回类型List - How to intercept the return type List using the spring AOP pointcut expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM