Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono<List<T>> difference with Flux<T> in Spring webflux

My understand is Mono<List<T>> is a synchronized Flux<T> and Flux could not be a rest api response.

Am I right?

If not, what's the different between Mono<List<T>> and Flux<T> or could a Flux could be a rest api response in some where ?

like image 288
zhuochen shen Avatar asked Oct 22 '18 10:10

zhuochen shen


1 Answers

  • as a return type, Mono<List<T>> means that you'll get asynchronously a full list of T elements in one shot.
  • Flux<T> means that you'll get zero to many T elements, possibly one by one as they come.

If you're getting such return types from an HTTP client such as WebClient, Mono<List<T>> and Flux<T> might be more or less equivalent from a runtime perspective, if the returned Content-Type is for example "application/json". In this case, the decoder will deserialize the response in one shot. The only different is, Flux<T> provides more interesting operators and you can always collectList and fall back to a Mono<List>.

On the other hand, if the returned Content-Type is a streaming one, for example "application/stream+json" then this definitely has an impact as you'll get the elements one by one as they come. In fact, if the returned stream is infinite, choosing Flux<T> is very important as the other one will never complete.

like image 110
Brian Clozel Avatar answered Sep 18 '22 22:09

Brian Clozel