Reaktive Controller
Erstellen Sie nicht blockierende Endpunkte mit WebFlux.
Reaktive Controller ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 2 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.
Reactive Web Endpoints
With WebFlux you write @RestController classes just like in Spring MVC, but methods return reactive types instead of plain objects.
- Return
Mono<T>for a single resource - Return
Flux<T>for a collection or stream
Returning a Mono
A handler returning Mono resolves to a single JSON object. The framework subscribes for you.
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public Mono<User> getUser(@PathVariable String id) {
return userService.findById(id);
}
}Returning a Flux
A handler returning Flux serializes to a JSON array by default.
@GetMapping
public Flux<User> getAllUsers() {
return userService.findAll();
}No Blocking Allowed
Inside reactive handlers you must not call blocking code. Return the publisher and let the chain stay reactive.
// BAD: blocks the event loop
@GetMapping("/bad")
public User bad() {
return userService.findById("1").block();
}
// GOOD: stays reactive
@GetMapping("/good")
public Mono<User> good() {
return userService.findById("1");
}Path and Query Params
Binding annotations work the same as in MVC: @PathVariable, @RequestParam, and @RequestBody.
@GetMapping("/search")
public Flux<User> search(@RequestParam String name) {
return userService.findByName(name);
}Accepting a Reactive Body
A POST handler can accept a Mono request body, keeping the whole flow non-blocking.
@PostMapping
public Mono<User> create(@RequestBody Mono<User> body) {
return body.flatMap(userService::save);
}Setting Status Codes
Use @ResponseStatus or wrap results in ResponseEntity within a Mono.
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Mono<User> create(@RequestBody Mono<User> body) {
return body.flatMap(userService::save);
}ResponseEntity in a Mono
To control headers and status dynamically, return Mono<ResponseEntity<T>>.
@GetMapping("/{id}")
public Mono<ResponseEntity<User>> getUser(@PathVariable String id) {
return userService.findById(id)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
}Streaming with Server-Sent Events
Set the content type to text/event-stream to stream a Flux to the client over time.
@GetMapping(value = "/stream",
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Long> stream() {
return Flux.interval(Duration.ofSeconds(1));
}Error Handling
Use onErrorResume to map errors to a fallback publisher, or a @ControllerAdvice for global handling.
@GetMapping("/{id}")
public Mono<User> getUser(@PathVariable String id) {
return userService.findById(id)
.onErrorResume(e -> Mono.error(
new ResponseStatusException(HttpStatus.NOT_FOUND)));
}The Netty Server
WebFlux runs on Netty by default — an event-loop server with a small fixed thread pool. This is why blocking calls are so harmful: blocking one thread stalls many requests.
Quick Check
Test your understanding of reactive controllers.
Recap
You learned how to build reactive endpoints:
@RestControllermethods returnMonoorFlux- Never call
block()inside a handler - Use
ResponseEntityin a Mono for dynamic status - Stream with
text/event-stream
Häufig gestellte Fragen
Ist die Lektion „Reaktive Controller“ kostenlos?
Ja — der vollständige Text von „Reaktive Controller“ 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 „Reaktive Controller“?
Erstellen Sie nicht blockierende Endpunkte mit WebFlux. 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 2 von 4.
Wie lange dauert die Lektion „Reaktive Controller“?
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