For the latest stable version, please use Spring Framework 6.2.6!spring-doc.cn

Return Values

The next table describes the supported controller method return values. Reactive types are supported for all return values.spring-doc.cn

Controller method return value Description

@ResponseBodyspring-doc.cn

The return value is converted through HttpMessageConverter implementations and written to the response. See @ResponseBody.spring-doc.cn

HttpEntity<B>, ResponseEntity<B>spring-doc.cn

The return value that specifies the full response (including HTTP headers and body) is to be converted through HttpMessageConverter implementations and written to the response. See ResponseEntity.spring-doc.cn

HttpHeadersspring-doc.cn

For returning a response with headers and no body.spring-doc.cn

ErrorResponsespring-doc.cn

To render an RFC 9457 error response with details in the body, see Error Responsesspring-doc.cn

ProblemDetailspring-doc.cn

To render an RFC 9457 error response with details in the body, see Error Responsesspring-doc.cn

Stringspring-doc.cn

A view name to be resolved with ViewResolver implementations and used together with the implicit model — determined through command objects and @ModelAttribute methods. The handler method can also programmatically enrich the model by declaring a Model argument (see Explicit Registrations).spring-doc.cn

Viewspring-doc.cn

A View instance to use for rendering together with the implicit model — determined through command objects and @ModelAttribute methods. The handler method can also programmatically enrich the model by declaring a Model argument (see Explicit Registrations).spring-doc.cn

java.util.Map, org.springframework.ui.Modelspring-doc.cn

Attributes to be added to the implicit model, with the view name implicitly determined through a RequestToViewNameTranslator.spring-doc.cn

@ModelAttributespring-doc.cn

An attribute to be added to the model, with the view name implicitly determined through a RequestToViewNameTranslator.spring-doc.cn

Note that @ModelAttribute is optional. See "Any other return value" at the end of this table.spring-doc.cn

ModelAndView objectspring-doc.cn

The view and model attributes to use and, optionally, a response status.spring-doc.cn

voidspring-doc.cn

A method with a void return type (or null return value) is considered to have fully handled the response if it also has a ServletResponse, an OutputStream argument, or an @ResponseStatus annotation. The same is also true if the controller has made a positive ETag or lastModified timestamp check (see Controllers for details).spring-doc.cn

If none of the above is true, a void return type can also indicate “no response body” for REST controllers or a default view name selection for HTML controllers.spring-doc.cn

DeferredResult<V>spring-doc.cn

Produce any of the preceding return values asynchronously from any thread — for example, as a result of some event or callback. See Asynchronous Requests and DeferredResult.spring-doc.cn

Callable<V>spring-doc.cn

Produce any of the above return values asynchronously in a Spring MVC-managed thread. See Asynchronous Requests and Callable.spring-doc.cn

ListenableFuture<V>, java.util.concurrent.CompletionStage<V>, java.util.concurrent.CompletableFuture<V>spring-doc.cn

Alternative to DeferredResult, as a convenience (for example, when an underlying service returns one of those).spring-doc.cn

ResponseBodyEmitter, SseEmitterspring-doc.cn

Emit a stream of objects asynchronously to be written to the response with HttpMessageConverter implementations. Also supported as the body of a ResponseEntity. See Asynchronous Requests and HTTP Streaming.spring-doc.cn

StreamingResponseBodyspring-doc.cn

Write to the response OutputStream asynchronously. Also supported as the body of a ResponseEntity. See Asynchronous Requests and HTTP Streaming.spring-doc.cn

Reactor and other reactive types registered via ReactiveAdapterRegistryspring-doc.cn

A single value type, e.g. Mono, is comparable to returning DeferredResult. A multi-value type, e.g. Flux, may be treated as a stream depending on the requested media type, e.g. "text/event-stream", "application/json+stream", or otherwise is collected to a List and rendered as a single value. See Asynchronous Requests and Reactive Types.spring-doc.cn

Other return valuesspring-doc.cn

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 BeanUtils#isSimpleProperty, in which case it remains unresolved.spring-doc.cn