小编Sim*_* B.的帖子

如何使用AsyncRestTemplate拦截AsyncClientHttpRequest?

我正在使用spring AsyncRestTemplatehelper类开发一个异步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);
Run Code Online (Sandbox Code Playgroud)

但是,如果由于某种原因我需要更改底层客户端,则不能再使用此拦截器.

是否有任何其他方法来拦截AsyncClientHttpRequest使用基础http客户端的拦截器不可知?

java spring

7
推荐指数
1
解决办法
906
查看次数

标签 统计

java ×1

spring ×1