14

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?

Aleksandr Filichkin
  • 660
  • 2
  • 8
  • 22

1 Answers1

13

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

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • 3
    So in case of a normal API exposed by a Spring RestController, assuming this API will expose a list of items (users' owned cars, for example), reading at your response I should choose `Mono>` instead of `Flux` because I'm just waiting for "1 time a value that is a list of objects". Is this correct? – Marco Apr 06 '21 at 07:03
  • The answer to your question is: yes, that's correct. – Marcos Aug 21 '21 at 07:33