Spring DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

这篇博客介绍了在遇到Spring DataBufferLimitException时的解决方法,包括通过配置参数和配置类加大缓冲区,限制接口返回JSON大小,检查网关拦截器的操作,并提供了一个另类的Hack AbstractDataBufferDecoder类的方法。
摘要由CSDN通过智能技术生成

Spring DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

前言

最近在项目中遇到了两次org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

问题1:REST API接口返回JSON超过256K时报错;

问题2:下载文件接口(返回二进制文件流,Content-Type为application/octet-stream)文件超过256K时报错。

Spring版本:

  • Spring Boot 2.2.2.RELEASE
  • Spring Core 5.2.2.RELEASE

问题分析

项目基于Spring Cloud Gateway来开发了API网关,上面的错误都发生在API网关。

从日志来看是典型的缓冲区溢出错误。

对问题1,只要加大缓冲区,或者限制返回JSON的大小就可以解决。

对问题2,写文件流的方式,按道理写入缓冲区的数据会被消费走,不应该出现缓冲区溢出错误。

问题解决过程

通过配置参数加大缓冲区

默认的缓冲区为256K,可以通过配置spring.codec.max-in-memory-size加大缓冲区:

spring:
  codec:
    max-in-memory-size: 2MB

关于该参数说明:

spring.codec.max-in-memory-size:

Limit on the number of bytes that can be buffered whenever the input stream needs to be aggregated. This applies only to the auto-configured WebFlux server and WebClient instances. By default this is not set, in which case individual codec defaults apply. Most codecs are limited to 256K by default.

不幸的是,该参数在Spring Boot 2.2.2.

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值