0Pricing
Spring Boot 4 Microservices & REST APIs · 课时

Mono 和 Flux 基础

了解响应式发布者

Mono 和 Flux 基础 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「Mono 和 Flux 基础」课时是免费的吗?

是的 — 「Mono 和 Flux 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。

「Mono 和 Flux 基础」这节课中我会学到什么?

了解响应式发布者 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Mono 和 Flux 基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?

能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Mono 和 Flux 基础
  2. 响应式控制器
  3. 使用 WebClient 进行响应式调用
  4. 背压和操作符
← 返回 Spring Boot 4 Microservices & REST APIs