spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.75k stars 38.15k forks source link

Improve buffering support in RestClient and make it easier to configure #33785

Open rstoyanchev opened 1 month ago

rstoyanchev commented 1 month ago

You can add a BufferingClientHttpRequestFactory wrapper around any request factory, but it forces you to create the underlying request factory, and you may not want to do that. For example, you might want to add request and response content logging, but not interfere with anything else.

Another issue is that the InterceptingClientHttpRequestFactory also buffers the request content. It is applied internally whenever interceptors are added. So if you want both buffering and interceptors, you end up with InterceptingClientHttpRequestFactory and BufferingClientHttpRequestFactory which duplicates request body buffering and copying.

We can make buffering easy to enable through the RestClient.Builder, and then support it as an internal matter just like we do for interceptors. This would allow us to ensure the request body is buffered efficiently once.

It should be possible to still decide on a per-request basis whether to buffer through some predicate or callback configured at the builder level.