-
- All Superinterfaces:
java.lang.AutoCloseable
public interface ResponsePayload extends java.lang.AutoCloseableA 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 ofResponsePayloadcan be acquired as the response body throughthe 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
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidclose()Makes sure the resources held by this payload are released.<T> ThandleWith(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)Converts this payload using the given body handler.<T> java.util.concurrent.CompletableFuture<T>handleWithAsync(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)Asynchronously converts this payload using the given body handler.booleanis(MediaType mediaType)Returns true if this payload has the given media type.default booleanisAnyOf(MediaType... mediaTypes)Returns true if this payload has any of the given media types.<T> Tto(TypeRef<T> typeRef)Converts this payload into an object of (possibly generic) typeT.default <T> Tto(java.lang.Class<T> type)Converts this payload into an object of typeT.<T> java.util.concurrent.CompletableFuture<T>toAsync(TypeRef<T> typeRef)Asynchronously converts this payload into an object of (possibly generic) typeT.default <T> java.util.concurrent.CompletableFuture<T>toAsync(java.lang.Class<T> type)Asynchronously converts this payload into an object of typeT.
-
-
-
Method Detail
-
is
boolean is(MediaType mediaType)
Returns true if this payload has the given media type.
-
isAnyOf
default boolean isAnyOf(MediaType... mediaTypes)
Returns true if this payload has any of the given media types.
-
to
default <T> T to(java.lang.Class<T> type) throws java.io.IOException, java.lang.InterruptedExceptionConverts this payload into an object of typeT.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
to
<T> T to(TypeRef<T> typeRef) throws java.io.IOException, java.lang.InterruptedException
Converts this payload into an object of (possibly generic) typeT.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
handleWith
<T> T handleWith(java.net.http.HttpResponse.BodyHandler<T> bodyHandler) throws java.io.IOException, java.lang.InterruptedExceptionConverts this payload using the given body handler.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
toAsync
default <T> java.util.concurrent.CompletableFuture<T> toAsync(java.lang.Class<T> type)
Asynchronously converts this payload into an object of typeT.
-
toAsync
<T> java.util.concurrent.CompletableFuture<T> toAsync(TypeRef<T> typeRef)
Asynchronously converts this payload into an object of (possibly generic) typeT.
-
handleWithAsync
<T> java.util.concurrent.CompletableFuture<T> handleWithAsync(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
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:
closein interfacejava.lang.AutoCloseable
-
-