javaspringreactive-programmingspring-webfluxreactor

How are Mono<Void> and Mono.empty() different


As per my understanding, in Spring WebFlux reactor

Mono<Void> refers for a void Mono

Mono.empty() refers to void, as calling anything over this gives a null pointer.

How do these stand different in their usage ?


Solution

  • Mono<T> is a generic type - in your specific situation it represents Void type as Mono<Void>

    Mono.empty() - return a Mono that completes without emitting any item.

    Let's assume that you got a method:

    private Mono<Void> doNothing() {
        return Mono.empty();
    }
    

    When you want to chain anything after the method call that returns Mono.empty() it won't work with flatMap as it is a completed Mono. In case you want continue another job after that method you can use operator then:

    doNothing().then(doSomething())