Search code examples
springrestspring-bootspring-rest

Spring RestTemplate - BufferingClientHttpRequestFactory & SimpleClientHttpRequestFactory


I saw the below code in one of the Rest Clients built using Spring. This Rest Client is present within a REST service and is calling another REST service. What is the purpose of this statement?

return new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())

Solution

  • BufferingClientHttpRequestFactory is a decorator around ClientHttpRequestFactory, which the RestTemplate uses to create ClientHttpRequests which faciliate HTTP communication. This decorator in particular provides buffering of outgoing/incoming streams. This wrapper/decorator also allows multiple reads of the response body, which you won't be able to do if you only use SimpleClientHttpRequestFactory or HttpComponentsClientHttpRequestFactory without this wrapper.

    SimpleClientHttpRequestFactory is an implementation of ClientHttpRequestFactory, which uses JDK facilities (classes from java.net package) and therefore does not depend on third party libraries, such as Apache HttpComponents HTTP client, which is required by another implementation HttpComponentsClientHttpRequestFactory.