15
votes

In Spring 5 I just know Spring WebFlux Handler method handles the request and returns Mono or Flux as response.

@Component
public class HelloWorldHandler {
    public Mono<ServerResponse> helloWorld(ServerRequest request) {
        return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World"));
    }
}

But I have no idea what means Mono and Flux and how it works with the WebFlux Handler.

Can any one simply explain

1.What means Mono and Flux.

2.How it works with the WebFlux Handler.

Thanks in advance.

1
@mrkernelpanic Here's a more up-to-date, and better, official docs page that explains in detail what Flux and Mono are: projectreactor.io/docs/core/release/reference/…tom_mai78101

1 Answers

34
votes

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.