0

I was trying to understand the use of InterceptingAsyncClientHttpRequestFactory. When run test with MockRestServiceServer I saw requestFactory is decorated with this ResquestFactory. Is there any other use of this requestFactory? Basically I want to know the idea behind InterceptingAsyncClientHttpRequestFactory. As I couldn't find any examples to use it. Below code doesn't work.

    AsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(
            httpAsyncClient);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(1);
    interceptors.add(asyncRestReqResInterceptor());
    AsyncClientHttpRequestFactory interceptorFactory = new InterceptingAsyncClientHttpRequestFactory(
            clientHttpRequestFactory, interceptors);
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(interceptorFactory);

Please let me know the correct implementation.

Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25

1 Answers1

0

The general idea behind request factory is that you can customize how HttpAccessor implementations, like RestTemplate, makes requests. If you don't specify anything you get non-pooled HttpURLConnection, but spring offers factories for Appache HttpClient and other 3rd part libraries.

InterceptingAsyncClientHttpRequestFactory is a decorator, which allows you to plug-in Interceptors that modify the request befor it is sent. One such Interceptor is the BasicAuthorizationInterceptor which adds the Authorization header to every request. If you have stuff you need to do to every request you can create your own ClientHttpRequestInterceptor.

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • Thank @Klaus. I understood this part. but still when I try to use it as shown in my updated question, it doesn't work. Also we can add the interceptor from `setInterceptors` method of asyncRestTemplate. – Vijendra Kumar Kulhade Feb 23 '17 at 18:15