Skip to content

Commit 09b8fea

Browse files
committed
Document streaming/collecting behavior for Flux return values
Closes gh-32630
1 parent d03ea0b commit 09b8fea

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Diff for: framework-docs/modules/ROOT/pages/web/webflux-reactive-libraries.adoc

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,17 @@
44
`spring-webflux` depends on `reactor-core` and uses it internally to compose asynchronous
55
logic and to provide Reactive Streams support. Generally, WebFlux APIs return `Flux` or
66
`Mono` (since those are used internally) and leniently accept any Reactive Streams
7-
`Publisher` implementation as input. The use of `Flux` versus `Mono` is important, because
8-
it helps to express cardinality -- for example, whether a single or multiple asynchronous
9-
values are expected, and that can be essential for making decisions (for example, when
10-
encoding or decoding HTTP messages).
7+
`Publisher` implementation as input.
8+
When a `Publisher` is provided, it can be treated only as a stream with unknown semantics (0..N).
9+
If, however, the semantics are known, you should wrap it with `Flux` or `Mono.from(Publisher)` instead
10+
of passing the raw `Publisher`.
11+
The use of `Flux` versus `Mono` is important, because it helps to express cardinality --
12+
for example, whether a single or multiple asynchronous values are expected,
13+
and that can be essential for making decisions (for example, when encoding or decoding HTTP messages).
1114

1215
For annotated controllers, WebFlux transparently adapts to the reactive library chosen by
1316
the application. This is done with the help of the
1417
{spring-framework-api}/core/ReactiveAdapterRegistry.html[`ReactiveAdapterRegistry`], which
1518
provides pluggable support for reactive library and other asynchronous types. The registry
1619
has built-in support for RxJava 3, Kotlin coroutines and SmallRye Mutiny, but you can
1720
register others, too.
18-
19-
For functional APIs (such as <<webflux-fn>>, the `WebClient`, and others), the general rules
20-
for WebFlux APIs apply -- `Flux` and `Mono` as return values and a Reactive Streams
21-
`Publisher` as input. When a `Publisher`, whether custom or from another reactive library,
22-
is provided, it can be treated only as a stream with unknown semantics (0..N). If, however,
23-
the semantics are known, you can wrap it with `Flux` or `Mono.from(Publisher)` instead
24-
of passing the raw `Publisher`.
25-
26-
For example, given a `Publisher` that is not a `Mono`, the Jackson JSON message writer
27-
expects multiple values. If the media type implies an infinite stream (for example,
28-
`application/json+stream`), values are written and flushed individually. Otherwise,
29-
values are buffered into a list and rendered as a JSON array.

Diff for: framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
[.small]#xref:web/webmvc/mvc-controller/ann-methods/return-types.adoc[See equivalent in the Servlet stack]#
55

66
The following table shows the supported controller method return values. Note that reactive
7-
types from libraries such as Reactor, RxJava, xref:web-reactive.adoc#webflux-reactive-libraries[or other] are
7+
types from libraries such as Reactor, RxJava, xref:web/webflux-reactive-libraries.adoc[or other] are
88
generally supported for all return values.
99

10+
For return types like `Flux`, when multiple values are expected, elements are streamed as they come
11+
and are not buffered. This is the default behavior, as keeping a potentially large amount of elements in memory
12+
is not efficient. If the media type implies an infinite stream (for example,
13+
`application/json+stream`), values are written and flushed individually. Otherwise,
14+
values are written individually and the flushing happens separately.
15+
16+
NOTE: If an error happens while an element is encoded to JSON, the response might have been written to and committed already
17+
and it is impossible at that point to render a proper error response.
18+
In some cases, applications can choose to trade memory efficiency for better handling such errors by buffering elements and encoding them all at once.
19+
Controllers can then return a `Flux<List<B>>`; Reactor provides a dedicated operator for that, `Flux#collectList()`.
20+
1021
[cols="1,2", options="header"]
1122
|===
1223
| Controller method return value | Description

0 commit comments

Comments
 (0)