How to intercept AsyncClientHttpRequest using AsyncRestTemplate?

I am developing an asynchronous REST client using the helper class spring AsyncRestTemplate .

The client must send a token in the header of each request.

You can add an interceptor when using the HttpAsyncClient ( http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html ) as the base http client of the rest of the template:

 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 base client, this interceptor can no longer be used.

Is there any other way to intercept AsyncClientHttpRequest using the main http client interceptor agnostic?

+5
source share
1 answer

No, not through AsyncRestTemplate , not through HttpAsyncClient . None of these interfaces provide a mutator for adding HttpRequestInterceptor instances.

As far as I know, only builders are mutable for them. Therefore, even if you can receive a factory request or a client, you still cannot change them.

You must intercept their actual creation, which, depending on your installation, may not be possible.

0
source

Source: https://habr.com/ru/post/1203926/


All Articles