liveBook · Manning

Mono

As explained in Spring Security in Action, Second Edition

Mono is a Reactor type representing an asynchronous computation that emits zero or one item.

Mono is a Project Reactor publisher representing an asynchronous computation that emits zero or one item. Reactive endpoints return it when they produce a single value or no value. section 17.1

Distinguish Mono from the surrounding reactive contracts:

  • Use Mono for a single result; use Flux for zero or more results. section 17.1
  • Treat an endpoint that returns data as a publisher consumed through Reactive Streams. section 17.1
  • Compose values with operators such as map instead of immediately extracting them. section 17.2

Returning and transforming a Mono

A controller can return Mono.just("Hello!"). When authentication is supplied as Mono<Authentication>, auth.map(a -> "Hello " + a.getName()) creates a Mono containing the personalized response. section 17.1 section 17.2

Listing 17.3 The HelloController class
@RestController
public class HelloController {

  @GetMapping("/hello")
  public Mono<String> hello(
    Mono<Authentication> auth) {      #1

    Mono<String> message =            #2
      auth.map(a -> "Hello " + a.getName());

    return message;
  }
}

Inspect a controller that receives Mono<Authentication> and maps its authentication value into a response. section 17.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines publisher cardinality and demonstrates map-based composition, while stopping short of detailed scheduling or operator internals.
Examples
Uses Mono.just for a greeting and Mono<Authentication> with map for a personalized authenticated response.
What it assumes
Assumes basic Spring and Java, while introducing reactive publishers, subscribers, and single-versus-multiple result modeling.
What you can do after
Build it from scratch in a WebFlux controller and compose emitted values with Reactor operators.
How it connects
Connects Mono with Flux, Reactive Streams, Publisher, Subscriber, Authentication, ReactiveUserDetailsService, and SecurityContext.

Also explained in

Compare how 2 books explain Mono →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.