Class MarshaledResponse
Response, suitable for sending to clients over the wire.
Your application's ResponseMarshaler is responsible for taking the Response returned by a Resource Method as input
and converting its Response.getBody() to a byte[].
For example, if a Response were to specify a body of List.of("one", "two"), a ResponseMarshaler might
convert it to the JSON string ["one", "two"] and provide as output a corresponding MarshaledResponse with a body of UTF-8 bytes that represent ["one", "two"].
Alternatively, your Resource Method might want to directly serve bytes to clients (e.g. an image or PDF) and skip the ResponseMarshaler entirely.
To accomplish this, just have your Resource Method return a MarshaledResponse instance: this tells Soklet "I already know exactly what bytes I want to send; don't go through the normal marshaling process".
Instances can be acquired via the withResponse(Response) or withStatusCode(Integer) builder factory methods.
Full documentation is available at https://www.soklet.com/docs/response-writing.
- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder used to construct instances ofMarshaledResponseviawithResponse(Response)orwithStatusCode(Integer).static final classBuilder used to copy instances ofMarshaledResponseviacopy(). -
Method Summary
Modifier and TypeMethodDescriptioncopy()Vends a mutable copier seeded with this instance's data, suitable for building new instances.Optional<byte[]> getBody()The HTTP response body to write, if available.The HTTP cookies to write for this response.The HTTP headers to write for this response.The HTTP status code for this response.toString()static MarshaledResponse.BuilderwithResponse(Response response) Acquires a builder forMarshaledResponseinstances.static MarshaledResponse.BuilderwithStatusCode(Integer statusCode) Acquires a builder forMarshaledResponseinstances.
-
Method Details
-
withResponse
Acquires a builder forMarshaledResponseinstances.- Parameters:
response- the logical response whose values are used to prime this builder- Returns:
- the builder
-
withStatusCode
Acquires a builder forMarshaledResponseinstances.- Parameters:
statusCode- the HTTP status code for this response- Returns:
- the builder
-
copy
Vends a mutable copier seeded with this instance's data, suitable for building new instances.- Returns:
- a copier for this instance
-
toString
-
getStatusCode
The HTTP status code for this response.- Returns:
- the status code
-
getHeaders
-
getCookies
The HTTP cookies to write for this response.- Returns:
- the cookies to write
-
getBody
The HTTP response body to write, if available.- Returns:
- the response body to write, or
Optional.empty()) if no body should be written
-