Spring Boot 4 Microservices & REST APIs · レッスン

MonoとFluxの基礎

リアクティブなPublisherを理解します

レッスン 1/413 ステップ

「MonoとFluxの基礎」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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
無料で開始

AI チューターと学ぶ Java — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
24
レッスン
93

よくある質問

「MonoとFluxの基礎」レッスンは無料ですか?

はい。「MonoとFluxの基礎」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「MonoとFluxの基礎」で何を学びますか?

リアクティブなPublisherを理解します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応の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に戻る