In Spring WebFlux, Mono is crucial for managing asynchronous data streams. Think of Mono like a reliable source that can provide either no data or just one piece of information. This makes it ideal for situations where you’re expecting either a single result or nothing at all. When we look at a practical “Spring Webflux Mono Example,” Mono’s significance becomes clearer. It shows how effectively it handles asynchronous data streams, which is essential for many real-world applications.
Mono: It can emit 0 or 1 item. Its like CompletableFuture with 0 or 1 result. It’s commonly used when you expect a single result or no result. For example, finding an entity by its ID or saving an entity.
Create a Spring Boot project and include the following dependency.
Example 1: Using Mono with CoreSubscriber
This example demonstrates the usage of Mono with a CoreSubscriber, where we create a Mono publisher with test data and subscribe to it. We handle different callback methods such as onSubscribe, onNext, onError, and onComplete to manage the data stream.
Example 2: Using Mono with various operators
Output:

This example showcases the usage of various Mono operators such as zipWith, zip, map, flatMap, flatMapMany, and concatWith. We create Monos with test data, subscribe to them, combine them using different operators, transform their data, and concatenate them.
Example 3: Writing Mono examples in a Controller
Example 4: Real-time Use Case with Spring WebFlux Mono Example:
Let’s consider a real-time example of fetching weather data from an external API using Mono, and then contrast it with a simple example without using Mono.
When we access the synchronous endpoint at http://localhost:8080/getWeatherDataSync, the output will be displayed immediately.
When we access the asynchronous endpoint at http://localhost:8080/getWeatherDataAsync, we will receive the weather data after other tasks have been completed.