I'm using Spring web-flux with Reactor and for me is not clear when RestController method should return
Mono <List<Object>>
and when Flux<Object>
.
Could you provide some case when to use each of them?
Mono is more relatable to the Optional class in Java since it contains 0 or 1 value, and Flux is more relatable to List since it can have N number of values.
Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types: Mono: implements Publisher and returns 0 or 1 elements. Flux: implements Publisher and returns N elements.
Convert Mono to Flux in Java To create a Flux from Mono, you can use the overloaded method from(), which accepts a Publisher as an argument.
Flux<Object>
indicates that new Object
instances can be pushed in a reactive way at any point. With Mono<List<Object>>
, you will get 1 time a value that is a list of objects, but that list will never change.
See also Mono vs Flux in Reactive Stream
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With