Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Mono<List<Object>> and when Flux<Object> for RestController method

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?

like image 524
Aleksandr Filichkin Avatar asked Oct 24 '18 09:10

Aleksandr Filichkin


People also ask

What is the difference between Flux and Mono?

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.

What is mono and Flux in WebFlux?

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.

How do you make Flux with mono?

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.


1 Answers

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

like image 124
Wim Deblauwe Avatar answered Nov 10 '22 19:11

Wim Deblauwe