ResponseEntity
ResponseEntity
is like @ResponseBody
but with status and headers. For example:
-
Java
-
Kotlin
@GetMapping("/something")
public ResponseEntity<String> handle() {
String body = ... ;
String etag = ... ;
return ResponseEntity.ok().eTag(etag).body(body);
}
@GetMapping("/something")
fun handle(): ResponseEntity<String> {
val body = ...
val etag = ...
return ResponseEntity.ok().eTag(etag).build(body)
}
Spring MVC supports using a single value reactive type
to produce the ResponseEntity
asynchronously, and/or single and multi-value reactive
types for the body. This allows the following types of async responses:
-
ResponseEntity<Mono<T>>
orResponseEntity<Flux<T>>
make the response status and headers known immediately while the body is provided asynchronously at a later point. UseMono
if the body consists of 0..1 values orFlux
if it can produce multiple values. -
Mono<ResponseEntity<T>>
provides all three — response status, headers, and body, asynchronously at a later point. This allows the response status and headers to vary depending on the outcome of asynchronous request handling.