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.
Convenience instance factories are also available via fromResponse(Response) and fromStatusCode(Integer).
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.static @NonNull MarshaledResponsefromResponse(@NonNull Response response) Creates aMarshaledResponsefrom a logicalResponsewithout additional customization.static @NonNull MarshaledResponsefromStatusCode(@NonNull Integer statusCode) Creates aMarshaledResponsewith the given status code and no additional customization.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()withResponse(@NonNull Response response) Acquires a builder forMarshaledResponseinstances.withStatusCode(@NonNull 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
-
fromResponse
Creates aMarshaledResponsefrom a logicalResponsewithout additional customization.- Parameters:
response- the logical response whose values are used to construct this instance- Returns:
- a
MarshaledResponseinstance
-
withStatusCode
Acquires a builder forMarshaledResponseinstances.- Parameters:
statusCode- the HTTP status code for this response- Returns:
- the builder
-
fromStatusCode
Creates aMarshaledResponsewith the given status code and no additional customization.- Parameters:
statusCode- the HTTP status code for this response- Returns:
- a
MarshaledResponseinstance
-
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
The HTTP headers to write for this response.Soklet writes one header line per value. If order matters, provide either a
SortedSetorLinkedHashSetto preserve the desired ordering; otherwise values are naturally sorted for consistency.- Returns:
- the headers to write
-
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
-