Spring WebFlux (Reactive Web)
The reactive, non-blocking web stack on Project Reactor: Mono and Flux, annotated and functional endpoints, WebClient, backpressure and the event-loop runtime. Interviewers use it to test whether you can justify reactive rather than just use it.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- Reactive Streams & Project Reactor30 questions
- Reactive Streams Specification5 questions
- Mono vs Flux Types5 questions
- Reactor Operators5 questions
- Schedulers & subscribeOn/publishOn5 questions
- Backpressure & Demand5 questions
- Reactor Error Handling5 questions
- Non-Blocking Runtime & Threading Model20 questions
- Reactor Netty Event Loop5 questions
- Event-Loop vs Thread-Per-Request5 questions
- Blocking the Event Loop5 questions
- DispatcherHandler Pipeline5 questions
- Annotated & Functional Endpoints30 questions
- Annotated Reactive Controllers5 questions
- RouterFunction & route()5 questions
- HandlerFunction & ServerResponse5 questions
- RequestPredicates5 questions
- Kotlin Coroutines & Flow Endpoints5 questions
- Reactive Multipart & File Upload5 questions
- Reactive WebClient19 questions
- retrieve vs exchangeToMono5 questions
- Request/Response Body Streaming4 questions
- ExchangeFilterFunction5 questions
- Status Handling, Timeouts & Retry5 questions
- Filters, Codecs, Errors & Streaming31 questions
- WebFilter & WebFilterChain5 questions
- Reactive Exception Handling5 questions
- Encoders, Decoders & CodecConfigurer5 questions
- SSE & Streaming Responses5 questions
- WebFluxConfigurer & @EnableWebFlux6 questions
- Reactive WebSocket Handling5 questions
- Reactive Context & Choosing WebFlux15 questions
- Reactor Context (contextWrite)5 questions
- Reactive Security Context Propagation5 questions
- Choosing WebFlux vs MVC5 questions
questions
145 · 6 sectionsWhat is backpressure in Project Reactor, and how does request(n) implement it?
basics
~20 sBackpressure lets a slow consumer control how fast a producer sends data. The subscriber calls request(n) on its Subscription to ask for at most n items; the publisher must never emit more than was requested.
In Project Reactor, what happens to a Flux or Mono when an error occurs, and why can't you just use a try/catch around it?
basics
~20 sAn error is a terminal signal: the stream emits onError, stops, and sends nothing more. try/catch doesn't work because the data flows asynchronously later, not on the line that builds the pipeline. You recover with operators like onErrorResume.
What is the difference between Mono and Flux in Project Reactor, and when would you use each?
basics
~20 sBoth are reactive publishers. Mono emits 0 or 1 item then completes; Flux emits 0 to many items then completes. Use Mono for a single result (like one entity), Flux for a stream or collection of items.
In Project Reactor, what is the difference between map and flatMap on a Flux or Mono?
basics
~10 smap transforms each item synchronously, one value in -> one value out. flatMap transforms each item into a Publisher (another Flux/Mono, often async) and flattens all those inner streams into one output stream.
What are the four interfaces defined by the Reactive Streams specification, and what is each responsible for?
basics
~10 sPublisher produces data, Subscriber consumes it, Subscription links the two and lets the Subscriber request items or cancel, and Processor is both a Subscriber and a Publisher (a middle stage).
In a Spring WebFlux app, what does it mean to 'block the event loop', and why is it a problem?
basics
~10 sIt means running a slow, waiting operation (like a JDBC query or Thread.sleep) directly on one of WebFlux's few event-loop threads. That thread can't handle other requests while it waits, so throughput collapses.
What is DispatcherHandler in Spring WebFlux, and what role does it play in request handling?
basics
~20 sDispatcherHandler is the central entry point in Spring WebFlux. For each request it finds the right handler (a controller method), invokes it, and turns the result into an HTTP response. It's the reactive equivalent of DispatcherServlet.
What server runs a Spring WebFlux application by default, and what kind of threads handles incoming requests?
basics
~10 sBy default WebFlux runs on Reactor Netty, an async non-blocking server. Requests are handled by a small pool of event-loop threads named reactor-http-nio-* instead of one thread per request.
What is the difference between the thread-per-request model of Spring MVC and the event-loop model of Spring WebFlux?
basics
~20 sSpring MVC gives each request its own thread that stays busy until the response is sent, so it uses many threads. WebFlux uses a few event-loop threads that handle many requests by never blocking and switching between them.
Why are block(), blockFirst(), and blockLast() forbidden on event-loop threads, and where can you legitimately call them?
basics
~10 sblock() waits (parks the thread) until the reactive stream finishes. On an event-loop thread that freezes request processing, so Reactor throws an error. It's fine in tests, main(), or plain non-reactive threads.
How do you write an annotated reactive controller in Spring WebFlux, and what changes compared to a Spring MVC controller?
basics
~10 sUse the same annotations as MVC (@RestController, @GetMapping, @PathVariable), but return reactive types: Mono<T> for zero-or-one value and Flux<T> for a stream of values, instead of a plain object.
How do you write a Spring WebFlux controller handler using Kotlin coroutines, and how do suspend functions and Flow<T> return types map onto the reactive model?
basics
~20 sMark the handler method suspend fun and return a plain value or a Flow<T> instead of Mono/Flux. Spring adapts a suspend function to a single-value response and a Flow to a streaming (many-value) response automatically.
What is a HandlerFunction<ServerResponse> in Spring WebFlux, and how does it differ from an annotated @RestController method?
basics
~10 sA HandlerFunction is a function that takes a ServerRequest and returns a Mono<ServerResponse>. It's the functional way to write a WebFlux endpoint, instead of using annotations like @GetMapping on a controller method.
How do you receive an uploaded file in a Spring WebFlux controller?
basics
~10 sBind the named multipart part with @RequestPart to a FilePart parameter. FilePart exposes filename(), headers(), and transferTo(path) to save the upload. Return a Mono/Flux so the handler stays non-blocking.
What are RequestPredicates in Spring WebFlux functional endpoints, and how do they connect an incoming request to a HandlerFunction?
basics
~10 sA RequestPredicate is a condition that tests an incoming request (its method, path, headers). In RouterFunctions.route(predicate, handler), if the predicate matches, that HandlerFunction runs and returns the response.
When sending a request body with WebClient, what is the difference between bodyValue(obj) and body(publisher, Class)? When would you use each?
basics
~20 sbodyValue takes a single already-available object and serializes it. body(publisher, Class) takes a reactive stream (Mono/Flux) that WebClient subscribes to and serializes element by element. Use bodyValue for one plain object, body(...) for reactive/streaming data.
What is an ExchangeFilterFunction in Spring WebClient, and how do you register one?
basics
~10 sIt is an interceptor for WebClient calls. It receives each outgoing request plus the next step in the chain, and can modify the request or response. You register it with WebClient.builder().filter(...).
How does WebClient handle non-2xx HTTP responses by default, and how do you customize that with onStatus?
basics
~10 sBy default, when you call retrieve(), WebClient turns 4xx/5xx responses into a WebClientResponseException error on the reactive stream. onStatus(predicate, handler) lets you intercept a status range and map it to your own exception.
How does retrieve() work with bodyToMono and bodyToFlux to decode a response?
basics
~10 sretrieve() triggers the request and gives you the response body directly. bodyToMono(Type.class) decodes a single object; bodyToFlux(Type.class) decodes a stream/collection into many elements. retrieve() auto-throws WebClientResponseException on 4xx/5xx.
What is Spring's WebClient and why is it the recommended replacement for RestTemplate?
basics
~20 sWebClient is Spring's non-blocking, reactive HTTP client. Unlike RestTemplate, which blocks a thread per request, WebClient uses async I/O and returns Mono/Flux, so a few threads handle many concurrent calls. RestTemplate is in maintenance mode.
In Spring WebFlux, what are Encoder/Decoder and HttpMessageReader/HttpMessageWriter, and what do they do?
basics
~10 sThey convert between Java objects and the raw bytes of an HTTP body. A Decoder/HttpMessageReader turns request bytes into objects; an Encoder/HttpMessageWriter turns response objects back into bytes (e.g. JSON).
How do you handle an exception thrown by a WebFlux controller method, both locally and globally?
basics
~10 sAdd a method annotated with @ExceptionHandler in the same controller to handle it locally. To handle exceptions from many controllers in one place, put @ExceptionHandler methods in a class annotated with @ControllerAdvice (or @RestControllerAdvice).
How do you expose a streaming endpoint in Spring WebFlux, and what does returning a Flux with produces=MediaType.TEXT_EVENT_STREAM_VALUE actually do?
basics
~10 sReturn a Flux from the controller and set produces=MediaType.TEXT_EVENT_STREAM_VALUE ("text/event-stream"). Spring keeps the HTTP response open and writes each emitted item to the client as a Server-Sent Event, instead of buffering one JSON array.
What is a Spring WebFlux `WebFilter`, and how does it relate to the servlet `Filter`?
basics
~10 sWebFilter is WebFlux's reactive interface for intercepting every request/response, the non-blocking equivalent of a servlet Filter. Its filter(exchange, chain) method returns Mono<Void>, and you call chain.filter(exchange) to continue to the next filter or handler.
What is WebFluxConfigurer and how do you use it to customize Spring WebFlux?
basics
~10 sWebFluxConfigurer is an interface with optional callback methods. You implement it on a @Configuration class to customize the reactive web setup — CORS, formatters, argument resolvers, codecs, content negotiation — without replacing Spring's defaults.
What is the Reactor Context and why does reactive code need it instead of ThreadLocal?
basics
~10 sReactor Context is a small immutable key-value map carried along a reactive pipeline. It replaces ThreadLocal because reactive operators hop between threads, so ThreadLocal values would be lost.
In a Spring WebFlux controller, how do you obtain the currently authenticated user, and why can't you just call SecurityContextHolder.getContext()?
basics
~10 sUse ReactiveSecurityContextHolder.getContext(), which returns a Mono<SecurityContext> you map to the Authentication/principal. The blocking SecurityContextHolder reads a ThreadLocal, which isn't reliably set on WebFlux's shared event-loop threads.
What is the core difference between Spring MVC and Spring WebFlux, and when would you pick each?
basics
~20 sMVC is blocking and uses one thread per request; WebFlux is non-blocking and reactive, handling many concurrent requests on a few threads. Pick WebFlux for high concurrency with slow I/O; MVC for simpler, mostly CPU or blocking-DB work.
How do you write to and read from the Reactor Context, and what is the difference between Context and ContextView?
basics
~10 sWrite with contextWrite() (returns a new immutable Context). Read with Mono/Flux.deferContextual, which hands you a ContextView. Context is the writable builder; ContextView is the read-only view operators receive.
Explain precisely why the ThreadLocal-based SecurityContextHolder breaks on WebFlux's event loop, and what replaces it.
basics
~20 sWebFlux uses a few event-loop threads that serve many requests and hop threads between operators. A ThreadLocal is bound to a thread, not a request, so it's empty or holds the wrong user. The Reactor Context (subscription-scoped) replaces it.