Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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())
like image 355
Punter Vicky Avatar asked Oct 27 '15 16:10

Punter Vicky


People also ask

What is BufferingClientHttpRequestFactory?

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.

Is RestTemplate getting deprecated?

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.

Should I use WebClient instead of RestTemplate?

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.


1 Answers

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.

like image 125
Bohuslav Burghardt Avatar answered Oct 01 '22 13:10

Bohuslav Burghardt