Spring Cloud Gateway来开发了API网关,REST API接口返回JSON超过256K时报错问题

22 篇文章 2 订阅
12 篇文章 0 订阅

查看Spring Core 5.1.14.RELEASE的AbstractDataBufferDecoder类,发现以下说明:

设置maxInMemorySize值的说明原因:

/**

*配置可随时缓冲的字节数限制

*需要聚合输入流。这可能是

*解码为单个{@code DataBuffer},

*{@链接java.nio.ByteBuffer文件ByteBuffer},{@code byte[]},

*{@链接org.springframework.core.io.资源资源}、{@code String}等。

*分割输入流时也会发生这种情况,例如分隔文本,

*在这种情况下,限制适用于分隔符之间缓冲的数据。

*<p>在5.1中,默认设置为-1,无限制。在5.2中,默认

*此限制的值设置为256K。

*@param byteCount缓冲区的最大字节数,或-1表示无限

*@自5.1.11

*/

 

源码如下:

public abstract class AbstractDataBufferDecoder<T> extends AbstractDecoder<T> {

    private int maxInMemorySize = -1;


    protected AbstractDataBufferDecoder(MimeType... supportedMimeTypes) {
        super(supportedMimeTypes);
    }


    /**
     * Configure a limit on the number of bytes that can be buffered whenever
     * the input stream needs to be aggregated. This can be a result of
     * decoding to a single {@code DataBuffer},
     * {@link java.nio.ByteBuffer ByteBuffer}, {@code byte[]},
     * {@link org.springframework.core.io.Resource Resource}, {@code String}, etc.
     * It can also occur when splitting the input stream, e.g. delimited text,
     * in which case the limit applies to data buffered between delimiters.
     * <p>By default in 5.1 this is set to -1, unlimited. In 5.2 the default
     * value for this limit is set to 256K.
     * @param byteCount the max number of bytes to buffer, or -1 for unlimited
     * @since 5.1.11
     */
    public void setMaxInMemorySize(int byteCount) {
        this.maxInMemorySize = byteCount;
    }

    /**
     * Return the {@link #setMaxInMemorySize configured} byte count limit.
     * @since 5.1.11
     */
    public int getMaxInMemorySize() {
        return this.maxInMemorySize;
    }

...

}

 

对比查看Spring Core 5.2.2.RELEASE的AbstractDataBufferDecoder类,发现虽然maxInMemorySize的默认值是262144,但是外部是可以调用它的setMaxInMemorySize()方法来设值的。但是不知道什么原因,设值就是不生效。

 

public abstract class AbstractDataBufferDecoder<T> extends AbstractDecoder<T> {
    private int maxInMemorySize = 262144;
    
    ...
    
    public void setMaxInMemorySize(int byteCount) {
        this.maxInMemorySize = byteCount;
    }

    public int getMaxInMemorySize() {
        return this.maxInMemorySize;
    }

    ...
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木鱼-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值