DataBufferLimitException:超出了缓冲 webflux 错误的最大字节数限制

2022-08-31 13:34:24

发送文件时,我收到一个字节数组。我总是遇到webflux接收数组的问题。抛出的错误如下:

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

你现在如何在网络流量中解决这个问题吗?


答案 1

这对我有用:

  1. 在其中一个配置类或主类中创建:@BeanSpringBootApplication

    @Bean
    public WebClient webClient() {
        final int size = 16 * 1024 * 1024;
        final ExchangeStrategies strategies = ExchangeStrategies.builder()
            .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
            .build();
        return WebClient.builder()
            .exchangeStrategies(strategies)
            .build();
    }
    
  2. 接下来,转到要使用 :WebClient

    @Service
    public class TestService {
    
        @Autowired
        private WebClient webClient;
    
        public void test() {
            String out = webClient
                .get()
                .uri("/my/api/endpoint")
                .retrieve()
                .bodyToMono(String.class)
                .block();
    
            System.out.println(out);
        }
    }
    

答案 2

我想这个问题是关于在Spring Boot中添加新的配置属性。将其添加到文件中,如下所示:spring.codec.max-in-memory-sizeapplication.yml

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

推荐