🔴 The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-02-20 19:15:22.410 ERROR 8842 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : Failed to handle HTTP request
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
at org.springframework.core.io.buffer.LimitedDataBufferList.updateCount(LimitedDataBufferList.java:80)
at org.springframework.core.io.buffer.DefaultDataBufferFactory.wrap(DefaultDataBufferFactory.java:85)⚡ Quick Fix Works 80% of the time
Increase the default in-memory buffer size for WebClient or Spring codecs.
# application.properties
spring.codec.max-in-memory-size=16MB🧠 Why this Happens
Tap to expand the deep technical explanation
Spring WebFlux defaults to buffering 262144 bytes (256KB) in memory to prevent Denial of Service (DoS) attacks. When a reactive endpoint tries to aggregate a large incoming JSON payload or a WebClient downloads a large file, it attempts to load the entire payload into a single memory buffer. If the payload exceeds 256KB, the buffer limit is breached, and the exception is thrown.
The HITEC City Parking Spot Analogy:
It's like a mailroom with a small package chute that rejects any box larger than a shoebox. If someone tries to shove a refrigerator box through it, the chute jams and rejects the package.
🔁 How to Reproduce Confirm this is your error
Create a Spring WebFlux app. Use `WebClient` to download a 1MB JSON file from a external API without configuring buffer sizes. The `bodyToMono(String.class)` call will trigger the exception.
🛠️ Solutions (5 Ways to Fix)
Increase the global buffer limit
👉 Use this when you need to handle larger standard JSON payloads across your app.
Set the property `spring.codec.max-in-memory-size` to increase the limit for all auto-configured WebClients and controllers.
# application.properties
spring.codec.max-in-memory-size=16MBConfigure WebClient ExchangeStrategies
👉 Use this when you want to increase the limit for a specific WebClient instance only.
Override the default `ExchangeStrategies` when building the WebClient bean.
@Bean
public WebClient webClient() {
return WebClient.builder()
.exchangeStrategies(ExchangeStrategies.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024))
.build())
.build();
}Stream the response instead of buffering
👉 Use this for very large files (e.g., video or CSV downloads) where buffering is impossible.
Instead of `bodyToMono` (which buffers), use `bodyToFlux` to stream chunks of data as they arrive, keeping memory usage flat.
// Instead of: .bodyToMono(String.class)
// Use:
.bodyToFlux(DataBuffer.class).map(buffer -> { ... });Use ResponseEntity<byte[]> for raw streaming
👉 Use this if you are passing the stream through a standard MVC controller.
Bypass the reactive codec limits by reading the raw bytes directly.
@GetMapping("/download")
public ResponseEntity<byte[]> download() {
byte[] data = webClient.get().retrieve().bodyToMono(byte[].class).block();
return ResponseEntity.ok(data);
}Write a custom ServerCodecConfigurer
👉 Use this for fine-grained control over specific endpoints.
Configure the codecs programmatically in a `WebFluxConfigurer`.
@Configuration
public class WebFluxConfig implements WebFluxConfigurer {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024);
}
}📋 Version Notes
Default limit is 256KB.
Default limit is 256KB. Stricter enforcement on reactive chains.
🛡️ How to Prevent This Next Time
For payloads over a few megabytes, always use streaming (`Flux<DataBuffer>`) instead of buffering. Reserve `max-in-memory-size` increases for medium-sized JSON payloads.