Search code examples
jsonspringmonostreamingspring-webflux

How to stream a Mono<list<Object>> through Spring webflux?


Spring webflux Json streaming is not working for a Flux object converted into Mono<list. It only receives a single response which results in AWS Timeout. Result service will call a reactive repository query it will return Flux and I want to convert it into a DAO object which is returning Mono<List<>>

  @GetMapping(value= "/resultMonoStream/{id}", produces = org.springframework.http.MediaType.APPLICATION_STREAM_JSON_VALUE)
             public ResponseEntity<Mono<List<ResultDao>>> getAllResultMonoStream() {
                    Flux<Result> result = resultService.getAllResult(id);
                    Mono<List<ResultDao>> response = result.map(entity-> entity.convertToFlatObject()).collect(Collectors.toList());
                return ResponseEntity.status(HttpStatus.OK).body(response);
            }

Solution

  • By definition

    • Mono is either 1 value or no value.

    • Flux can contain from 0 up to N values.

    It only receives a single response

    Because Mono is handled as a single element. So by definition mostly 1 element can be streamed.