In Spring WebFlux, @Controller marks a class as a web controller exactly as it does in Spring MVC, but the handler methods run on a reactive, non-blocking stack. They can return Mono<T> for zero or one value and Flux<T> for a stream of values, and the framework subscribes to them and writes the response as data arrives. The annotation itself is the same; what changes is the programming model underneath.
Use @RestController for JSON APIs and @Controller when you render reactive views such as Thymeleaf templates. The examples below target Spring Framework 6 and Spring Boot 3 or later.
@Controller means in WebFlux@Controller@Controller means in WebFlux@Controller is a stereotype annotation meta-annotated with @Component. Component scanning registers the class as a bean, and WebFlux's RequestMappingHandlerMapping (the reactive variant in org.springframework.web.reactive.result.method.annotation) discovers its @RequestMapping, @GetMapping and @PostMapping methods.
The annotation is shared with Spring MVC, so the same class compiles in both stacks. Which stack runs depends on the dependency you add: spring-boot-starter-web gives you Spring MVC on servlets, and spring-boot-starter-webflux gives you WebFlux, which runs on Netty by default.
A handler method can return plain objects, but the real value comes from returning reactive types from Project Reactor.
findOne returns a Mono, so WebFlux serialises a single JSON object. findAll returns a Flux, which becomes a JSON array. stream uses text/event-stream, so each element is sent to the client as a server-sent event as soon as it is ready, without holding a thread while waiting.
@ControllerWhen you use @Controller without @ResponseBody, a returned String is again a view name. WebFlux resolves it with a reactive ViewResolver, for example the one from Thymeleaf.
The template engine resolves the Flux while rendering, so the controller does not need to block. Do not call block() in a handler: it can throw on a Netty event-loop thread and defeats the point of WebFlux.
WebFlux offers a second way to define routes, using RouterFunction and HandlerFunction. Both styles run on the same reactive infrastructure.
| Aspect | Annotated controllers | Functional endpoints |
|---|---|---|
| Style | @Controller, @GetMapping | RouterFunctions.route(...) |
| Familiarity | Same as Spring MVC | New API |
| Validation and binding | Built in with annotations | You call the validator yourself |
| Best for | Most applications | Small services and explicit routing |
You can mix both in one application. Most teams migrating from Spring MVC start with annotated controllers because existing knowledge carries over.
Thread.sleep or a blocking HTTP client stall event-loop threads. Use R2DBC, WebClient or move the blocking work to Schedulers.boundedElastic().HttpServletRequest are not available. Use ServerWebExchange or ServerHttpRequest instead.ThreadLocal based context does not follow a request across threads. Use the Reactor Context, or Micrometer context propagation.@Controller the same in Spring MVC and WebFlux?It is the same annotation, but WebFlux handler methods can return Mono and Flux, take ServerWebExchange, and never depend on the Servlet API.
@Controller or @RestController in WebFlux?Use @RestController for JSON or other response-body APIs, and @Controller for pages rendered from templates.
Mono?Yes, WebFlux accepts it, but you lose the non-blocking benefit if producing that object blocks. Prefer Mono or Flux end to end.
Spring MVC can also return Mono and Flux from controllers, but only WebFlux runs fully non-blocking on Netty or another reactive server.
@Controller registers a class as a web controller in WebFlux just as in Spring MVC. The difference is the reactive model: return Mono and Flux, keep the whole chain non-blocking, and choose @RestController for APIs or @Controller for reactive view rendering.