Java – how to use asyncresttemplate to intercept asyncclienthttprequest?
I am using the spring asyncresttemplate helper class to develop asynchronous rest clients
The client needs to send a token in the header of each request
Using httpasyncclient( http://hc.apache.org/httpcomponents-asyncclient-4.0.x/index.html )As the base HTTP client of the remaining templates, you can add interceptors:
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, I can no longer use this interceptor
Is there any other way to intercept asyncclienthttprequest using the interceptor of the underlying HTTP client unknown?
Solution
No, not through asyncresttemplate, but through httpasyncclient None of these interfaces provide mutators for adding httprequestinterceptor instances
As far as I know, only these builders are changeable Therefore, even if you can get request factories or clients, you cannot change them
You have to intercept their actual creation, depending on your settings, which may not be possible