DataBufferLimitException: Exceeded limit on max bytes to buffer webflux error

前端 未结 5 842
悲哀的现实
悲哀的现实 2021-01-01 15:29

While sending a file I receive an array of bytes. I always have a problem with webflux to receive an array. the error thrown as below :

org.springframework.co         


        
5条回答
  •  情歌与酒
    2021-01-01 15:47

    Instead of retrieving data at once, you can stream:

    Mono string = webClient.get()
        .uri("end point of an API")
        .retrieve()
        .bodyToFlux(DataBuffer.class)
        .map(buffer -> {
            String string = buffer.toString(Charset.forName("UTF-8"));
            DataBufferUtils.release(buffer);
            return string;
        });
    

    Alternatively convert to stream:

        .map(b -> b.asInputStream(true))
        .reduce(SequenceInputStream::new)
        .map(stream -> {
            // consume stream
            stream.close();
            return string;
        });
    

    In most cases you don't want to really aggregate the stream, rather than processing it directly. The need to load huge amount of data in memory is mostly a sign to change the approach to more reactive one. JSON- and XML-Parsers have streaming interfaces.

提交回复
热议问题