Root Cause
Spring WebClient includes a default codec memory buffer limit to prevent application memory exhaustion.
This limit is set to 256KB by default. DataBufferLimitException occurs when the response payload exceeds this threshold.
Resolution
To resolve DataBufferLimitException, increase the codec memory buffer limit using the maxInMemorySize parameter during WebClient configuration.
ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024)) // 10MB
.build();
WebClient webClient = WebClient.builder()
.exchangeStrategies(exchangeStrategies)
.build();
As documented in CodecConfigurer#maxInMemorySize, you can also remove the limit by passing -1.
Also note that the exchangeStrategies method that accepts a Consumer parameter is deprecated, as shown below.
Use the approach above that passes an ExchangeStrategies instance directly.
WebClient webClient = WebClient.builder()
.exchangeStrategies(builder -> // Deprecated approach
builder.codecs(codecs ->
codecs.defaultCodecs().maxInMemorySize(10 * 1024 * 1024)
)
).build();