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; T is usually inferred from the method's return type.
build<T>() → ResponseEntity<T>
Finishes the response without a body; write build<Book>() when T cannot be inferred.
contentType(String mediaType) → ResponseEntityBuilder
Sets the Content-Type header, such as MediaType.applicationJson. Without it, the server picks the type from the body and the request's Accept header.
eTag(String tag) → ResponseEntityBuilder
Sets the ETag header, a version tag of the body for caching; tag is put in quotes unless it already is (W/"..." included). The web server answers a GET or HEAD whose If-None-Match has the tag with a 304 and no body, when the status is 200 (Spring's HttpEntityMethodProcessor).
Adds values to the header name, 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-Modified header, written as an HTTP date in UTC. The web server answers a GET or HEAD with an If-Modified-Since at or after it (and no If-None-Match) with a 304, and one with an If-Unmodified-Since before it with a 412, when the status is 200.
location(Uri location) → ResponseEntityBuilder
Sets the Location header: 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