skip to content

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 pageshow

explore

questions

145 · 6 sections

What is backpressure in Project Reactor, and how does request(n) implement it?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Backpressure 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.

open as a page

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?

level: juniorimportance: must knowfreq 70%
basics
~20 s

An 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.

open as a page

What is the difference between Mono and Flux in Project Reactor, and when would you use each?

level: juniorimportance: must knowfreq 85%
basics
~20 s

Both 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.

open as a page

In Project Reactor, what is the difference between map and flatMap on a Flux or Mono?

level: juniorimportance: must knowfreq 85%
basics
~10 s

map 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.

open as a page

What are the four interfaces defined by the Reactive Streams specification, and what is each responsible for?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Publisher 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).

open as a page

In a Spring WebFlux app, what does it mean to 'block the event loop', and why is it a problem?

level: juniorimportance: must knowfreq 78%
basics
~10 s

It 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.

open as a page

What is DispatcherHandler in Spring WebFlux, and what role does it play in request handling?

level: juniorimportance: must knowfreq 70%
basics
~20 s

DispatcherHandler 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.

open as a page

What server runs a Spring WebFlux application by default, and what kind of threads handles incoming requests?

level: juniorimportance: must knowfreq 70%
basics
~10 s

By 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.

open as a page

What is the difference between the thread-per-request model of Spring MVC and the event-loop model of Spring WebFlux?

level: juniorimportance: must knowfreq 85%
basics
~20 s

Spring 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.

open as a page

Why are block(), blockFirst(), and blockLast() forbidden on event-loop threads, and where can you legitimately call them?

level: middleimportance: must knowfreq 70%
basics
~10 s

block() 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.

open as a page

How do you write an annotated reactive controller in Spring WebFlux, and what changes compared to a Spring MVC controller?

level: juniorimportance: must knowfreq 75%
basics
~10 s

Use 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.

open as a page

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?

level: juniorimportance: must knowfreq 60%
basics
~20 s

Mark 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.

open as a page

What is a HandlerFunction<ServerResponse> in Spring WebFlux, and how does it differ from an annotated @RestController method?

level: juniorimportance: must knowfreq 60%
basics
~10 s

A 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.

open as a page

How do you receive an uploaded file in a Spring WebFlux controller?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Bind 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.

open as a page

What are RequestPredicates in Spring WebFlux functional endpoints, and how do they connect an incoming request to a HandlerFunction?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A 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.

open as a page

When sending a request body with WebClient, what is the difference between bodyValue(obj) and body(publisher, Class)? When would you use each?

level: juniorimportance: must knowfreq 70%
basics
~20 s

bodyValue 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.

open as a page

What is an ExchangeFilterFunction in Spring WebClient, and how do you register one?

level: juniorimportance: must knowfreq 60%
basics
~10 s

It 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(...).

open as a page

How does WebClient handle non-2xx HTTP responses by default, and how do you customize that with onStatus?

level: juniorimportance: must knowfreq 70%
basics
~10 s

By 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.

open as a page

How does retrieve() work with bodyToMono and bodyToFlux to decode a response?

level: juniorimportance: must knowfreq 70%
basics
~10 s

retrieve() 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.

open as a page

What is Spring's WebClient and why is it the recommended replacement for RestTemplate?

level: juniorimportance: must knowfreq 78%
basics
~20 s

WebClient 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.

open as a page

In Spring WebFlux, what are Encoder/Decoder and HttpMessageReader/HttpMessageWriter, and what do they do?

level: juniorimportance: must knowfreq 55%
basics
~10 s

They 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).

open as a page

How do you handle an exception thrown by a WebFlux controller method, both locally and globally?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Add 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).

open as a page

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?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Return 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.

open as a page

What is a Spring WebFlux `WebFilter`, and how does it relate to the servlet `Filter`?

level: juniorimportance: must knowfreq 68%
basics
~10 s

WebFilter 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.

open as a page

What is WebFluxConfigurer and how do you use it to customize Spring WebFlux?

level: juniorimportance: must knowfreq 55%
basics
~10 s

WebFluxConfigurer 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.

open as a page

What is the Reactor Context and why does reactive code need it instead of ThreadLocal?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Reactor 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.

open as a page

In a Spring WebFlux controller, how do you obtain the currently authenticated user, and why can't you just call SecurityContextHolder.getContext()?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Use 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.

open as a page

What is the core difference between Spring MVC and Spring WebFlux, and when would you pick each?

level: juniorimportance: must knowfreq 75%
basics
~20 s

MVC 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.

open as a page

How do you write to and read from the Reactor Context, and what is the difference between Context and ContextView?

level: middleimportance: must knowfreq 60%
basics
~10 s

Write 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.

open as a page

Explain precisely why the ThreadLocal-based SecurityContextHolder breaks on WebFlux's event loop, and what replaces it.

level: middleimportance: must knowfreq 65%
basics
~20 s

WebFlux 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.

open as a page