This part of the documentation covers support for reactive stack, web applications built on a Reactive Streams API to run on non-blocking servers such as Netty, Undertow, and Servlet 3.1+ containers. Individual chapters cover the Spring WebFlux framework, the reactive WebClient, support for Testing, and Reactive Libraries. For Servlet stack, web applications, please see Web on Servlet Stack.
The spring-test module provides mock implementations of ServerHttpRequest,
ServerHttpResponse, and ServerWebExchange.
See Spring Web Reactive mock objects.
The WebTestClient builds on these mock request and
response objects to provide support for testing WebFlux applications without and HTTP
server. The WebTestClient can be used for end-to-end integration tests too.
Reactor is a required dependency for the spring-webflux module and is used internally
for composing logic and for Reactive Streams support. An easy rule to remember is that
WebFlux APIs return Flux or Mono — since that’s what’s used internally, and
leniently accept any Reactive Streams Publisher implementation.
The use of Flux and Mono helps to express cardinality — e.g.
whether a single or multiple async values are expected. This is important for API design
but also essential in some cases, e.g. when encoding an HTTP message.
For annotated controllers, WebFlux adapts transparently to the reactive library in use
with proper translation of cardinality. This is done with the help of the
ReactiveAdapterRegistry from
spring-core which provides pluggable support for reactive and async types. The registry
has built-in support for RxJava and CompletableFuture but others can be registered.
For functional endpoints, the WebClient, and other functional APIs, the general rule
of thumb for WebFlux APIs applies:
-
FluxorMonoas return values — use them to compose logic or pass to any Reactive Streams library (both arePublisherimplementations). -
Reactive Streams
Publisherfor input — if aPublisherfrom another reactive library is provided it can only be treated as a stream with unknown semantics (0..N). If the semantics are known — e.g.io.reactivex.Single, you can useMono.from(Publisher)and pass that in instead of the rawPublisher.
|
Note
|
For example, given a |