2017-09-14 09:02:28 +08:00
|
|
|
[[spring-web-reactive]]
|
|
|
|
= Web on Reactive Stack
|
|
|
|
:doc-root: https://docs.spring.io
|
|
|
|
:api-spring-framework: {doc-root}/spring-framework/docs/{spring-version}/javadoc-api/org/springframework
|
|
|
|
:toc: left
|
|
|
|
:toclevels: 4
|
2017-11-21 05:28:00 +08:00
|
|
|
:tabsize: 4
|
2017-09-14 09:02:28 +08:00
|
|
|
:docinfo1:
|
|
|
|
|
2019-03-05 20:08:34 +08:00
|
|
|
This part of the documentation covers support for reactive-stack web applications built
|
2019-03-21 06:48:14 +08:00
|
|
|
on a https://www.reactive-streams.org/[Reactive Streams] API to run on non-blocking
|
2018-09-17 22:36:43 +08:00
|
|
|
servers, such as Netty, Undertow, and Servlet 3.1+ containers. Individual chapters cover
|
2020-01-24 04:01:58 +08:00
|
|
|
the <<webflux.adoc#webflux, Spring WebFlux>> framework,
|
2019-03-05 20:08:34 +08:00
|
|
|
the reactive <<webflux-client, `WebClient`>>, support for <<webflux-test, testing>>,
|
|
|
|
and <<webflux-reactive-libraries, reactive libraries>>. For Servlet-stack web applications,
|
|
|
|
see <<web.adoc#spring-web, Web on Servlet Stack>>.
|
2017-09-14 09:02:28 +08:00
|
|
|
|
2017-10-06 10:43:26 +08:00
|
|
|
include::web/webflux.adoc[leveloffset=+1]
|
|
|
|
|
|
|
|
include::web/webflux-webclient.adoc[leveloffset=+1]
|
|
|
|
|
2017-11-14 07:06:10 +08:00
|
|
|
include::web/webflux-websocket.adoc[leveloffset=+1]
|
2017-10-06 10:43:26 +08:00
|
|
|
|
2017-10-19 02:24:17 +08:00
|
|
|
|
|
|
|
|
2017-10-06 10:43:26 +08:00
|
|
|
[[webflux-test]]
|
|
|
|
== Testing
|
2019-03-05 20:08:34 +08:00
|
|
|
[.small]#<<web.adoc#testing, Same in Spring MVC>>#
|
2017-10-06 10:43:26 +08:00
|
|
|
|
|
|
|
The `spring-test` module provides mock implementations of `ServerHttpRequest`,
|
|
|
|
`ServerHttpResponse`, and `ServerWebExchange`.
|
2019-03-05 20:08:34 +08:00
|
|
|
See <<testing.adoc#mock-objects-web-reactive, Spring Web Reactive>> for a
|
|
|
|
discussion of mock objects.
|
2017-10-06 10:43:26 +08:00
|
|
|
|
2019-03-05 20:08:34 +08:00
|
|
|
<<testing.adoc#webtestclient, `WebTestClient`>> builds on these mock request and
|
2018-09-17 22:36:43 +08:00
|
|
|
response objects to provide support for testing WebFlux applications without an HTTP
|
|
|
|
server. You can use the `WebTestClient` for end-to-end integration tests, too.
|
2017-10-06 10:43:26 +08:00
|
|
|
|
|
|
|
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2019-09-23 19:17:26 +08:00
|
|
|
include::rsocket.adoc[leveloffset=+1]
|
2018-03-16 10:13:24 +08:00
|
|
|
|
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2017-10-06 10:43:26 +08:00
|
|
|
[[webflux-reactive-libraries]]
|
|
|
|
== Reactive Libraries
|
|
|
|
|
2018-03-16 10:13:24 +08:00
|
|
|
`spring-webflux` depends on `reactor-core` and uses it internally to compose asynchronous
|
2018-09-17 22:36:43 +08:00
|
|
|
logic and to provide Reactive Streams support. Generally, WebFlux APIs return `Flux` or
|
|
|
|
`Mono` (since those are used internally) and leniently accept any Reactive Streams
|
2019-03-05 20:08:34 +08:00
|
|
|
`Publisher` implementation as input. The use of `Flux` versus `Mono` is important, because
|
|
|
|
it helps to express cardinality -- for example, whether a single or multiple asynchronous
|
|
|
|
values are expected, and that can be essential for making decisions (for example, when
|
|
|
|
encoding or decoding HTTP messages).
|
2018-03-16 10:13:24 +08:00
|
|
|
|
|
|
|
For annotated controllers, WebFlux transparently adapts to the reactive library chosen by
|
|
|
|
the application. This is done with the help of the
|
2018-09-17 22:36:43 +08:00
|
|
|
{api-spring-framework}/core/ReactiveAdapterRegistry.html[`ReactiveAdapterRegistry`], which
|
2018-03-16 10:13:24 +08:00
|
|
|
provides pluggable support for reactive library and other asynchronous types. The registry
|
2018-09-17 22:36:43 +08:00
|
|
|
has built-in support for RxJava and `CompletableFuture`, but you can register others, too.
|
2018-03-16 10:13:24 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
For functional APIs (such as <<webflux-fn>>, the `WebClient`, and others), the general rules
|
|
|
|
for WebFlux APIs apply -- `Flux` and `Mono` as return values and a Reactive Streams
|
2018-03-16 10:13:24 +08:00
|
|
|
`Publisher` as input. When a `Publisher`, whether custom or from another reactive library,
|
2018-09-17 22:36:43 +08:00
|
|
|
is provided, it can be treated only as a stream with unknown semantics (0..N). If, however,
|
2018-03-16 10:13:24 +08:00
|
|
|
the semantics are known, you can wrap it with `Flux` or `Mono.from(Publisher)` instead
|
|
|
|
of passing the raw `Publisher`.
|
2017-10-06 10:43:26 +08:00
|
|
|
|
|
|
|
For example, given a `Publisher` that is not a `Mono`, the Jackson JSON message writer
|
2018-09-17 22:36:43 +08:00
|
|
|
expects multiple values. If the media type implies an infinite stream (for example,
|
|
|
|
`application/json+stream`), values are written and flushed individually. Otherwise,
|
2017-10-06 10:43:26 +08:00
|
|
|
values are buffered into a list and rendered as a JSON array.
|