org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:101)Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException
If you see the error message “DataBufferLimitException: Exceeded limit on max bytes,” it means that a program or system has reached a maximum limit on the amount of data it can handle at once. This issue can occur when dealing with large amounts of information.
When using WebClient or WebTestClient and encountering this error then increase the buffer limit.
In order in increase buffer limit of webclient then we need to take help of ExchangeStrategies class.
ExchangeStrategies class have codecs which contain “maxInMemorySize” where we can set the buffer size based on our requirements.
final int size = 16 * 1024 * 1024; // equivalent to 16 MB
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
maxInMemorySize(size) : here size is of int bytes.
Now once we get strategies object then we can set this in WebClient or WebTestClient.
1 . WebClient :
WebClient.builder.exchangeStrategies(strategies).build();
2. WebTestClient :
WebTestClient does not provide direct builder function , in order to use it we have to use mutate() function which allows builder() properties.
webTestClient.mutate() .exchangeStrategies(strategies) .build();
The provided code, by adjusting the buffer limit in WebClient or WebTestClient, aims to resolve the DataBufferLimitException.
The given solution is generic and can be applied to resolve the issue. Simply include the provided code in your Spring Boot main class.
@Bean public WebClient getWebClient() {
return WebClient.builder() // Creating a WebClient using the builder pattern
.baseUrl("Your_SERVICE_URL") // Setting the base URL for the WebClient
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(16 * 1024 * 1024)) // Configuring codecs, setting max in-memory size
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) // Setting default content type header
.build(); // Building and returning the configured WebClient instance
}
DataBufferLimitException Solution Explanation :
WebClient.builder(): Initiates the creation of a WebClient instance using the builder pattern. The builder pattern allows for step-by-step construction and configuration of an object. .baseUrl(“Your_SERVICE_URL”): Sets the base URL for the WebClient. Replace “Your_SERVICE_URL” with the actual URL of the service you want to communicate with.
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)):
codecs(…): Configures the codecs used for encoding and decoding requests and responses.
configurer ->: A lambda expression defining a configuration function for codecs.
defaultCodecs(): Specifies the use of default codecs provided by Spring.
maxInMemorySize(16 * 1024 * 1024): Sets the maximum in-memory buffer size for reading and writing data. In this case, it’s set to 16 megabytes (16 * 1024 * 1024 bytes).
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE): Sets default headers for the WebClient.
HttpHeaders.CONTENT_TYPE: Specifies the header key for content type. MediaType.APPLICATION_JSON_VALUE: Sets the default content type to JSON.
.build(): Completes the configuration and builds the WebClient instance with the specified settings.
The configured WebClient is then returned by the method.
Thank you !!
By reading this article, you can enhance your knowledge and develop your programming skills. We’ve provided insightful suggestions and specific examples for resolving issue of DataBufferLimitException.
Please feel free to share any other approaches or methods you may have with us by posting a comment or emailing technonamecontact@gmail.com. Your code can be highlighted on our website.
To further improve your understanding of programming concepts please check out our Data structure and algorithms section.
If you found this article informative and helpful, please share it with your friends and colleagues. If you have any questions or feedback, please feel free to leave a comment or contact us.
Follow us on Twitter for daily updates and new content.