In Spring WebFlux, @RestController works exactly as it does in Spring MVC: it combines @Controller and @ResponseBody, so handler return values become the response body instead of view names. The difference is the return types. WebFlux handlers return Mono<T> for zero or one value and Flux<T> for many, and the framework writes them to the response without blocking a thread.
This keeps the familiar annotated programming model while running on a reactive, non-blocking stack, typically Netty, with Spring Boot 3.x or 4.x.
The annotation is a meta-annotation that applies @Controller (so the class is picked up by component scanning) and @ResponseBody (so return values are serialized by an HttpMessageWriter). Methods use the usual mapping annotations: @GetMapping, @PostMapping, @PathVariable, @RequestBody and so on.
The annotation does not make the code reactive on its own. Reactivity comes from the types you return and from the libraries you call, such as R2DBC or WebClient.
| Return type | Meaning | Response |
|---|---|---|
Mono<T> | Zero or one item | One JSON object, or an empty response |
Flux<T> | Zero to many items | JSON array, or a stream with a streaming media type |
Mono<Void> | Completion only | No body |
Mono<ResponseEntity<T>> | Item plus status and headers | Full control of the response |
T (plain) | Allowed, but computed eagerly | Works, but loses non-blocking benefits |
This example uses a record and an in-memory store to stay self-contained. Replace the store with a reactive repository in a real service.
@RequestBody Mono<NewBook> lets the framework decode the body without blocking, though a plain NewBook parameter is also supported. Nothing runs until Spring subscribes to the returned publisher.
Returning a Flux with text/event-stream produces Server-Sent Events, which is a natural fit for live updates.
Each emitted value is sent as its own event as soon as it is ready.
WebFlux also offers functional endpoints with RouterFunction and HandlerFunction. Both models run on the same reactive core.
| Model | Style | Choose it when |
|---|---|---|
@RestController | Annotations, familiar from MVC | Your team knows Spring MVC, or you are migrating |
| Functional endpoints | Lambdas and route builders | You want routes as explicit code |
Also consider whether WebFlux is needed at all. On Java 21 and later, virtual threads let ordinary blocking Spring MVC code scale to many concurrent requests with simpler code. WebFlux remains a strong choice for streaming, backpressure and fully reactive pipelines.
Thread.sleep, blocking HTTP clients) on the event loop. Use reactive drivers or offload to Schedulers.boundedElastic().block() inside a controller; it defeats the model and can throw on reactor threads.spring-boot-starter-web and spring-boot-starter-webflux expecting WebFlux to run; with both present, Boot starts the servlet stack.The annotation is the same. The runtime differs: WebFlux supports Mono and Flux return types and runs non-blocking.
Yes, and Spring wraps it, but the work to produce it is done before the response is handled. Return Mono or Flux to keep the pipeline reactive.
Return Mono<ResponseEntity<T>>, use @ResponseStatus, or signal an error with ResponseStatusException.
Pick WebFlux for streaming and reactive end-to-end stacks. Pick MVC with virtual threads when your code is mostly blocking I/O and you want simpler debugging.
@RestController in WebFlux is @Controller plus @ResponseBody with reactive return types. Return Mono and Flux, avoid blocking calls, and use text/event-stream for streaming. The annotated model stays close to Spring MVC, so skills carry over directly.