- All Superinterfaces:
AutoCloseable
A response body that is yet to be handled into the desirable type. This can be useful when the
response body differs in structure (and hence in high-level type) according to whether it has
succeeded or failed. An implementation of
ResponsePayload
can be acquired as the response
body through the basic decoder
. See example below.
To avoid resource leaks, use this type within a try-with-resources construct immediately after sending the response (regardless of whether the body is converted synchronously or asynchronously):
var client =
Methanol.newBuilder()
.adapterCodec(AdapterCodec.newBuilder().basic().build()) // Install basic adapters.
.build();
var response = client.send(MutableRequest.GET("https://example.com"), ResponsePayload.class);
try(var body = response.body()) {
if (HttpStatus.isSuccessful(response) {
System.out.println(body.to(SuccessType.class));
} else if (HttpStatus.isClientError(response)) {
System.out.println(body.to(ErrorType.class));
} else {
// Discard body.
}
}
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Makes sure the resources held by this payload are released.<T> T
handleWith
(HttpResponse.BodyHandler<T> bodyHandler) Converts this payload using the given body handler.<T> CompletableFuture
<T> handleWithAsync
(HttpResponse.BodyHandler<T> bodyHandler) Asynchronously converts this payload using the given body handler.boolean
Returns true if this payload has the given media type.default boolean
Returns true if this payload has any of the given media types.<T> T
Converts this payload into an object of (possibly generic) typeT
.default <T> T
Converts this payload into an object of typeT
.<T> CompletableFuture
<T> Asynchronously converts this payload into an object of (possibly generic) typeT
.default <T> CompletableFuture
<T> Asynchronously converts this payload into an object of typeT
.
-
Method Details
-
is
Returns true if this payload has the given media type. -
isAnyOf
Returns true if this payload has any of the given media types. -
to
Converts this payload into an object of typeT
.- Throws:
IOException
InterruptedException
-
to
Converts this payload into an object of (possibly generic) typeT
.- Throws:
IOException
InterruptedException
-
handleWith
Converts this payload using the given body handler.- Throws:
IOException
InterruptedException
-
toAsync
Asynchronously converts this payload into an object of typeT
. -
toAsync
Asynchronously converts this payload into an object of (possibly generic) typeT
. -
handleWithAsync
Asynchronously converts this payload using the given body handler. -
close
void close()Makes sure the resources held by this payload are released. If the payload has been consumed, this method does nothing.- Specified by:
close
in interfaceAutoCloseable
-