Grundlagen von Mono und Flux
Verstehen Sie reaktive Publisher.
Grundlagen von Mono und Flux ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 itemFlux<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 runsTransforming 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); // -1Subscribe 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:
Monoemits 0 or 1 item;Fluxemits 0 to N- Publishers are lazy — nothing runs until
subscribe() mapandfilterdescribe transformationsonErrorReturnprovides fallbacks
Lerne Java mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 24
- Lektionen
- 93
Häufig gestellte Fragen
Ist die Lektion „Grundlagen von Mono und Flux“ kostenlos?
Ja — der vollständige Text von „Grundlagen von Mono und Flux“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Grundlagen von Mono und Flux“?
Verstehen Sie reaktive Publisher. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Grundlagen von Mono und Flux“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Grundlagen von Mono und Flux
- Reaktive Controller
- WebClient für reaktive Aufrufe
- Backpressure und Operatoren