0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Mono and Flux Basics

Understand reactive publishers.

Mono and Flux Basics is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Reactive Programming

Reactive programming is about building non-blocking, asynchronous applications that handle streams of data.

Instead of returning a value directly, a reactive method returns a publisher that emits data over time. The caller subscribes to receive it.

  • Threads are not blocked waiting for I/O
  • Great for high-concurrency, low-latency services

Project Reactor

Spring WebFlux is built on Project Reactor, which provides two core publisher types:

  • Mono<T> — emits 0 or 1 item
  • Flux<T> — emits 0 to N items

Both implement the Reactive Streams Publisher interface.

Creating a Mono

A Mono represents a single (or empty) async result. Use Mono.just() for a known value and Mono.empty() for none.

Mono<String> mono = Mono.just("Hello");
Mono<String> empty = Mono.empty();
mono.subscribe(value -> System.out.println(value));

Creating a Flux

A Flux emits many items. Build one from values, an iterable, or a range.

Flux<String> flux = Flux.just("a", "b", "c");
Flux<Integer> range = Flux.range(1, 5);
flux.subscribe(item -> System.out.println(item));

Nothing Happens Until Subscribe

Publishers are lazy. The pipeline does nothing until something subscribes.

Calling map() or filter() only describes work. The data flows only when subscribe() is invoked.

Flux<Integer> flux = Flux.range(1, 3)
    .map(i -> i * 10);
// no output yet
flux.subscribe(System.out::println); // now it runs

Transforming with map

map applies a synchronous function to each emitted item, transforming it one-to-one.

Flux<String> names = Flux.just("alice", "bob")
    .map(name -> name.toUpperCase());
names.subscribe(System.out::println);

Filtering Items

filter keeps only items that match a predicate.

Flux<Integer> evens = Flux.range(1, 10)
    .filter(n -> n % 2 == 0);
evens.subscribe(System.out::println);

Mono from a Supplier

Use Mono.fromSupplier() to defer execution of a value-producing function until subscription time.

Mono<Long> now = Mono.fromSupplier(() -> System.currentTimeMillis());
now.subscribe(t -> System.out.println("Time: " + t));

Handling Errors

Reactive pipelines signal errors as a terminal event. Use onErrorReturn to supply a fallback value.

Mono<Integer> result = Mono.just("abc")
    .map(Integer::parseInt)
    .onErrorReturn(-1);
result.subscribe(System.out::println); // -1

Subscribe Callbacks

The full subscribe form accepts three callbacks: onNext, onError, and onComplete.

Flux.just(1, 2, 3)
    .subscribe(
        item -> System.out.println("Next: " + item),
        error -> System.err.println("Error: " + error),
        () -> System.out.println("Done")
    );

Blocking for Tests

In tests or simple mains you can convert reactive types to blocking ones with block() or blockFirst().

Avoid block() in production reactive code — it defeats the non-blocking model.

String value = Mono.just("Hi").block();
Integer first = Flux.range(1, 5).blockFirst();

Quick Check

Test your understanding of Mono and Flux.

Recap

You learned the foundations of reactive programming with Project Reactor:

  • Mono emits 0 or 1 item; Flux emits 0 to N
  • Publishers are lazy — nothing runs until subscribe()
  • map and filter describe transformations
  • onErrorReturn provides fallbacks

Frequently asked questions

Is the “Mono and Flux Basics” lesson free?

Yes — the full text of “Mono and Flux Basics” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Mono and Flux Basics”?

Understand reactive publishers. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mono and Flux Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Mono and Flux Basics
  2. Reactive Controllers
  3. WebClient for Reactive Calls
  4. Backpressure and Operators
← Back to Spring Boot 4 Microservices & REST APIs