Spring WebFlux Setup — Controllers, Functional Routing, WebClient
Fast recall
Quick revision
Core answer
@RestController returning Mono / Flux) or via functional routing (RouterFunction + HandlerFunction). For outbound calls, use `WebClient` — never RestTemplate — with explicit timeouts, connection pool tuning, and retry. Add cross-cutting concerns via WebFilter, secure with @EnableWebFluxSecurity, configure CORS via CorsWebFilter, serve SSE by returning Flux> , and test with WebTestClient plus StepVerifier. Never block on the event-loop thread; wrap any blocking call in Schedulers.boundedElastic().Practical setup, awareness of annotated vs functional styles, WebClient hygiene, and observability hooks
Using RestTemplate in WebFlux; default WebClient without timeouts or pool config; mixing blocking libraries on the event loop
Discuss functional routing trade-offs, WebFilter ordering, per-upstream WebClient pools, SSE streaming, BlockHound in tests
Interview-ready guide
Interview answer
WebFlux is the reactive counterpart to Spring MVC. Add spring-boot-starter-webflux and you get Reactor plus Netty by default.
Two styles
- Annotated controllers —
@RestControllerreturningMono/Flux; quickest to onboard - Functional routing —
RouterFunction+HandlerFunction; explicit and composable; good for gateways
Endpoint notes
switchIfEmpty(Mono.error(NotFoundException))is the idiomatic 404- SSE —
Fluxwith> produces = TEXT_EVENT_STREAM_VALUE @Valid @RequestBody Monofor reactive validation
WebClient — outbound done right
- Always
WebClient, neverRestTemplate - One bean per upstream — separate pools, timeouts, retries
responseTimeout, connect timeout, read / write timeoutonStatusfor 4xx / 5xx mappingretryWhen(Retry.backoff(...))filtered to retryable errors- Circuit breaker via Resilience4j
WebFilter for cross-cutting
- Implements
WebFilter; useContext.ofto propagate trace IDs into the reactive chain @Ordercontrols execution- For security, prefer
SecurityWebFilterChain
Security
@EnableWebFluxSecurityandSecurityWebFilterChainauthorizeExchangewith path matchersoauth2ResourceServer.jwtfor JWT
CORS
CorsWebFilter, notCorsFilter
Testing
WebTestClientwith@SpringBootTest(webEnvironment = RANDOM_PORT)StepVerifierfor reactive streams- BlockHound in tests catches accidental blocking
Common gotchas
RestTemplatein WebFlux blocks the event loop- Blocking JDBC needs
Mono.fromCallable(...).subscribeOn(boundedElastic()) - One
WebClientper upstream; never share with different configs - CORS is
CorsWebFilternotCorsFilter - Never
.block()in the handler path
Deployment
- Executable jar;
java -jar - Container with
eclipse-temurin:21-jre-alpine - Readiness / liveness probes via Actuator
- Prometheus and OpenTelemetry exporters
Strategic answer
- Annotated controllers for most teams; functional routing for gateways
- WebClient configuration is where most production bugs hide — invest there
- Observability is auto-wired but must be exposed and watched
- BlockHound in CI is cheap insurance
Spring WebFlux Setup — Controllers, Functional Routing, WebClient
Direct Answer
A WebFlux application uses spring-boot-starter-webflux, which brings Reactor plus Netty by default. You write endpoints as annotated controllers (@RestController returning Mono / Flux) or via functional routing (RouterFunction + HandlerFunction). For outbound calls, use WebClient — never RestTemplate — with explicit timeouts, connection pool tuning, and retry. Add cross-cutting concerns via WebFilter, secure with @EnableWebFluxSecurity, configure CORS via CorsWebFilter, serve SSE by returning Flux<ServerSentEvent<T>>, and test with WebTestClient plus StepVerifier. Never block on the event-loop thread; wrap any blocking call in Schedulers.boundedElastic().
Common Mistakes & Pitfalls
Using RestTemplate in WebFlux; default WebClient without timeouts or pool config; mixing blocking libraries on the event loop
Check your understanding
Answer these without looking back at the article.
- Annotated controllers vs functional routing — when to choose each?
- How do you tune WebClient connection pool and timeouts per upstream?
- How do you serve Server-Sent Events with WebFlux and test them?
- What changes in Spring Security when moving from MVC to WebFlux?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.