Nozioni di base su Mono e Flux
Comprenda i publisher reattivi.
Nozioni di base su Mono e Flux è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Impara Java con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 24
- Lezioni
- 93
Domande Frequenti
La lezione «Nozioni di base su Mono e Flux» è gratuita?
Sì — il testo completo di «Nozioni di base su Mono e Flux» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «Nozioni di base su Mono e Flux»?
Comprenda i publisher reattivi. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Nozioni di base su Mono e Flux»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Nozioni di base su Mono e Flux
- Controller reattivi
- WebClient per chiamate reattive
- Backpressure e operatori