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())
BufferingClientHttpRequestFactory is a decorator around ClientHttpRequestFactory , which the RestTemplate uses to create ClientHttpRequest s which faciliate HTTP communication. This decorator in particular provides buffering of outgoing/incoming streams.
WebClient offers support for both synchronous and asynchronous HTTP requests and streaming scenarios. Therefore, RestTemplate will be marked as deprecated in a future version of the Spring Framework and will not contain any new functionalities. RestTemplate is based on a thread-per-request model.
Even on the official Spring documentation, they advise to use WebClient instead. WebClient can basically do what RestTemplate does, making synchronous blocking calls. But it also has asynchronous capabilities, which makes it interesting. It has a functional way of programming, which makes it easy to read as well.
BufferingClientHttpRequestFactory
is a decorator around ClientHttpRequestFactory
, which the RestTemplate uses to create ClientHttpRequest
s 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With