Module methanol

Interface ResponsePayload

All Superinterfaces:
AutoCloseable

public interface ResponsePayload extends 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 Type
    Method
    Description
    void
    Makes sure the resources held by this payload are released.
    <T> T
    Converts this payload using the given body handler.
    Asynchronously converts this payload using the given body handler.
    boolean
    is(MediaType mediaType)
    Returns true if this payload has the given media type.
    default boolean
    isAnyOf(MediaType... mediaTypes)
    Returns true if this payload has any of the given media types.
    <T> T
    to(TypeRef<T> typeRef)
    Converts this payload into an object of (possibly generic) type T.
    default <T> T
    to(Class<T> type)
    Converts this payload into an object of type T.
    toAsync(TypeRef<T> typeRef)
    Asynchronously converts this payload into an object of (possibly generic) type T.
    default <T> CompletableFuture<T>
    toAsync(Class<T> type)
    Asynchronously converts this payload into an object of type T.