ResponseEntityBuilder class final
The builder returned by ResponseEntity.status, ResponseEntity.created and the other shortcuts: the status is set, headers are added one by one, and body or build makes the ResponseEntity.
It saves building an HttpHeaders object by hand when a response needs a header or two. Each header method returns the builder, so the calls chain.
// An image with caching headers, so browsers can revalidate it.
@GetMapping('/books/{isbn}/cover')
Future<ResponseEntity<Uint8List>> cover(@PathVariable() String isbn) async {
final cover = await covers.find(isbn);
return ResponseEntity.status(HttpStatus.ok)
.contentType(MediaType.imagePng)
.eTag(cover.hash)
.lastModified(cover.updatedAt)
.body(cover.bytes);
}
Spring (Java): ResponseEntity.BodyBuilder.
return ResponseEntity.status(HttpStatus.OK)
.contentType(MediaType.IMAGE_PNG)
.eTag(cover.hash())
.lastModified(cover.updatedAt())
.body(cover.bytes());
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
body<
T> (T? body) → ResponseEntity< T> -
Finishes the response with
body;Tis usually inferred from the method's return type. -
build<
T> () → ResponseEntity< T> -
Finishes the response without a body; write
build<Book>()whenTcannot be inferred. -
contentType(
String mediaType) → ResponseEntityBuilder -
Sets the
Content-Typeheader, such asMediaType.applicationJson. Without it, the server picks the type from the body and the request'sAcceptheader. -
eTag(
String tag) → ResponseEntityBuilder -
Sets the
ETagheader, a version tag of the body for caching;tagis put in quotes unless it already is (W/"..."included). The web server answers aGETorHEADwhoseIf-None-Matchhas the tag with a 304 and no body, when the status is 200 (Spring'sHttpEntityMethodProcessor). -
header(
String name, List< String> values) → ResponseEntityBuilder -
Adds
valuesto the headername, after any values it already has:.header('Cache-Control', ['no-store']). -
headers(
HttpHeaders headers) → ResponseEntityBuilder -
Copies every header of
headers; a name already set on the builder gets the new values instead of its old ones. -
lastModified(
DateTime date) → ResponseEntityBuilder -
Sets the
Last-Modifiedheader, written as an HTTP date in UTC. The web server answers aGETorHEADwith anIf-Modified-Sinceat or after it (and noIf-None-Match) with a 304, and one with anIf-Unmodified-Sincebefore it with a 412, when the status is 200. -
location(
Uri location) → ResponseEntityBuilder -
Sets the
Locationheader: where the new or moved resource is. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited