R
realNameHidden
Guest

Normally, in Spring Boot (MVC), when you write:
Code:
@GetMapping("/hello")
public String sayHello() {
return "Hello World!";
}

The request β executes the method β response is returned immediately.

Sometimes data doesnβt come instantly (e.g., fetching from DB, calling another API, adding a delay).
Instead of blocking the thread, we want to return a βpromiseβ that will complete later.
Thatβs where Mono comes in.
Think of Mono as a box that will contain ONE value in the future.

Code:
@GetMapping("/hello")
public Mono<String> sayHello() {
return Mono.just("Hello Reactive World!");
}

Mono.just("Hello Reactive World!") means:
"I promise I will give you this string β just not necessarily right now."
When the browser calls /hello, Spring WebFlux unwraps the Mono and sends the string.

Code:
@GetMapping("/delayed")
public Mono<String> delayedHello() {
return Mono.just("Hello after delay!")
.delayElement(Duration.ofSeconds(2)); // respond after 2 seconds
}

The response comes after 2 seconds, but the server doesnβt block the thread during the wait.
That means the server can serve other requests meanwhile.

Code:
record User(String id, String name) {}
@GetMapping("/user")
public Mono<User> getUser() {
User user = new User("101", "Alice");
return Mono.just(user);
}
}

Code:
{
"id": "101",
"name": "Alice"
}
}

Think of Mono as:
A parcel delivery service

You place an order β you get a tracking ID immediately (Mono).
The actual parcel (data) may come later.
When it arrives, you open it and see the item.
So, Mono = βa container for ONE thing that comes now or later.β

Your API returns a single item asynchronously.
You want non-blocking performance in Spring WebFlux.
Continue reading...