From 71e2035d06d0b37345e8233f098e0001795b028a Mon Sep 17 00:00:00 2001 From: verils Date: Wed, 5 Nov 2025 19:03:00 +0800 Subject: [PATCH] Add return types description Signed-off-by: verils --- .../web/webflux/controller/ann-methods/responseentity.adoc | 2 ++ .../web/webflux/controller/ann-methods/return-types.adoc | 7 +++++++ .../modules/ROOT/pages/web/webflux/reactive-spring.adoc | 6 ++++++ .../modules/ROOT/pages/web/webmvc/message-converters.adoc | 5 +++++ .../webmvc/mvc-controller/ann-methods/return-types.adoc | 3 +++ 5 files changed, 23 insertions(+) diff --git a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/responseentity.adoc b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/responseentity.adoc index 6831075da2a1..fd5b46277fa9 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/responseentity.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/responseentity.adoc @@ -33,6 +33,8 @@ Kotlin:: ---- ====== +A `ResponseEntity` can be returned for file content, as same as in Spring MVC, + WebFlux supports using a single value xref:web-reactive.adoc#webflux-reactive-libraries[reactive type] to produce the `ResponseEntity` asynchronously, and/or single and multi-value reactive types for the body. This allows a variety of async responses with `ResponseEntity` as follows: diff --git a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc index f859c50e4de3..e6ff1984cf89 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc @@ -84,6 +84,13 @@ Reactor provides a dedicated operator for that, `Flux#collectList()`. to be written (however, `text/event-stream` must be requested or declared in the mapping through the `produces` attribute). +| `Flux` +| Write raw data buffers to the response, for example, when handling file + downloads or proxying data. + +| `Resource` +| Return a single resource that is written to the response using xref:web/webflux/reactive-spring.adoc#webflux-codecs-resource[ResourceHttpMessageWriter]. + | Other return values | If a return value remains unresolved in any other way, it is treated as a model attribute, unless it is a simple type as determined by diff --git a/framework-docs/modules/ROOT/pages/web/webflux/reactive-spring.adoc b/framework-docs/modules/ROOT/pages/web/webflux/reactive-spring.adoc index 084ebba4aa2f..ac9377ca5052 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux/reactive-spring.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux/reactive-spring.adoc @@ -611,6 +611,12 @@ and from higher level objects, or unless they choose to create custom codecs. Fo cases please review the information in xref:core/databuffer-codec.adoc[Data Buffers and Codecs], especially the section on xref:core/databuffer-codec.adoc#databuffers-using[Using DataBuffer]. +[[webflux-codecs-resource]] +=== `Resource` + +`Resource` is a general abstraction for file-like resources in Spring. WebFlux applications can use the `ResourceHttpMessageReader` and `ResourceHttpMessageWriter` to read and write `Resource` instances to and from HTTP requests and responses. + +Notice, the `ResourceHttpMessageWriter` automatically detects the media type by the filename of the `Resource` and uses it to override your response's `Content-Type` header. If the detection fails, `application/octet-stream` is used as a fallback. Don't return `Resource` type if you want to control the media type of the response. [[webflux-logging]] == Logging diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/message-converters.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/message-converters.adoc index 176ae223eee8..ec85e2b3f66f 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/message-converters.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/message-converters.adoc @@ -83,4 +83,9 @@ content type. This requires the `com.google.protobuf:protobuf-java` dependency. | An `HttpMessageConverter` implementation that can read and write JSON documents to and from Protobuf messages. This requires the `com.google.protobuf:protobuf-java-util` dependency. +| `ResourceHttpMessageConverter` +| An `HttpMessageConverter` implementation that can read and write `org.springframework.core.io.Resource` instances from the HTTP request and response. + +Notice, the `ResourceHttpMessageWriter` automatically detects the media type by the filename of the `Resource` and uses it to override your response's `Content-Type` header. If the detection fails, `application/octet-stream` is used as a fallback. Don't return `Resource` type if you want to control the media type of the response. + |=== diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc index 29bf34c7b91d..d5cdb1135f5b 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc @@ -95,6 +95,9 @@ supported for all return values. collected to a List and rendered as a single value. See xref:web/webmvc/mvc-ann-async.adoc[Asynchronous Requests] and xref:web/webmvc/mvc-ann-async.adoc#mvc-ann-async-reactive-types[Reactive Types]. +| `Resource` +| Return a single resource that is written to the response using `ResourceHttpMessageConverter` implementations. + | Other return values | If a return value remains unresolved in any other way, it is treated as a model attribute, unless it is a simple type as determined by