Webflux is all about reactive programming, which in summary means that business logic is only executed as soon as the data to process it is available (reactive).
This means you no longer can return simple POJO's, but you have to return something else, something that can provide the result when it's available. Within the reactive streams initiative, this is called a Publisher
. A Publisher
has a subcribe()
method that will allow the consumer to get the POJO when it's available.
A Publisher
(for example Publisher<Foo>
) can return zero or multiple, possibly infinite, results. To make it more clear how many results you can expect, Project Reactor (the reactive streams implementation of Pivotal) introduced two implementations of Publisher
:
- A
Mono
, which will complete after emitting a single result.
- A
Flux
, which will emit zero or multiple, possibly infinite, results and then completes.
So, basically you can see Mono<Foo>
as the reactive counterpart of returning Foo
and Flux<Foo>
as the reactive counterpart of Collection<Foo>
.
For example:
Flux
.just(1, 2, 3, 4)
.map(nr -> nr * 2)
.subscribe(System.out::println);
Even though the numbers are already available (you can see them), you should realize that since it's a Flux
, they're emitted one by one. In other cases, the numbers might come from an external API and in that case they won't be immediately available.
The next phase (the map
operator), will multiply the number as soon as it retrieves one, this means that it also does this mapping one by one and then emit the new value.
Eventually, there's a subscriber (there should always be one, but it could be the Spring framework itself that's subscribing), and in this case it will print each value it obtains and print it to the console, also, one by one.
You should also realize that there's no particular order when processing these items. It could be that the first number has already been printed on the console, while the third item is hasn't been multiplied by two yet.
So, in your case, you have a Mono<ServerResponse>
, which means that as soon as the ServerResponse
is available, the WebFlux framework can utilize it. Since there is only one ServerResponse
expected, it's a Mono
and not a Flux
.
Flux
andMono
are: projectreactor.io/docs/core/release/reference/… – tom_mai78101