java – 如何使用AsyncRestTemplate拦截AsyncClientHttpRequest
发布时间:2020-12-15 00:33:56 所属栏目:Java 来源:网络整理
导读:我正在使用 spring AsyncRestTemplate帮助程序类开发异步REST客户端. 客户端需要在每个请求的标头中发送令牌. 使用HttpAsyncClient(http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html)作为其余模板的基础http客户端时,可以添加拦截器: Htt
我正在使用
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); 但是,如果由于某种原因我需要更改底层客户端,则不能再使用此拦截器. 是否有任何其他方法来拦截AsyncClientHttpRequest使用基础http客户端的拦截器不可知? 解决方法
不,不是通过AsyncRestTemplate而是通过HttpAsyncClient.这些接口都没有提供用于添加HttpRequestInterceptor实例的mutator.
据我所知,只有这些的建设者是可变的.因此,即使您可以获得请求工厂或客户端,您仍然无法更改它们. 你必须拦截他们的实际创作,这取决于你的设置,可能是不可能的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |