Interface HttpClientResponse


public interface HttpClientResponse
Controls how the response is processed including potential conversion into beans.
  • Method Details

    • async

      Send the request async using CompletableFuture.

      Example async().bean()

      In this example POST async that will return a bean converted from json response.

      
      
          client.request()
             ...
             .POST().async()
             .bean(HelloDto.class)
             .whenComplete((helloDto, throwable) -> {
      
               if (throwable != null) {
                 HttpException httpException = (HttpException) throwable.getCause();
                 int statusCode = httpException.statusCode();
      
                 // maybe convert json error response body to a bean (using Jackson/Gson)
                 MyErrorBean errorResponse = httpException.bean(MyErrorBean.class);
                 ..
      
               } else {
                 // process helloDto
                 ...
               }
             });
       
    • call

      Return a HttpCall which allows either sync or async execution of the request.
    • read

      <T> T read(BodyReader<T> reader)
      Returning the response using the given response reader.
      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      reader - The response reader.
      Returns:
      The response converted into the appropriate bean via the reader.
      Throws:
      HttpException - when the response has error status codes
    • as

      <T> HttpResponse<T> as(Class<T> type)
      Return the response with the body containing a single instance of the given type.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • as

      <T> HttpResponse<T> as(Type type)
      Return the response with the body containing a single instance of the given type.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • asList

      <T> HttpResponse<List<T>> asList(Class<T> type)
      Return the response with the body containing a list of the given type.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • asList

      <T> HttpResponse<List<T>> asList(Type type)
      Return the response with the body containing a list of the given type.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • asStream

      <T> HttpResponse<Stream<T>> asStream(Class<T> type)
      Return the response with the body containing a stream of beans of the given type.

      Typically the response is expected to be application/x-json-stream newline delimited json payload.

      Note that for this stream request the response content is not deemed 'loggable' by avaje-http-client. This is because the entire response may not be available at the time of the callback. As such RequestLogger will not include response content when logging stream request/response

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • asStream

      <T> HttpResponse<Stream<T>> asStream(Type type)
      Return the response with the body containing a stream of beans of the given type.

      Typically the response is expected to be application/x-json-stream newline delimited json payload.

      Note that for this stream request the response content is not deemed 'loggable' by avaje-http-client. This is because the entire response may not be available at the time of the callback. As such RequestLogger will not include response content when logging stream request/response

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The response containing the converted body.
      Throws:
      HttpException - when the response has error status codes
    • bean

      <T> T bean(Class<T> type)
      Return the response as a single bean.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The bean the response is converted into.
      Throws:
      HttpException - when the response has error status codes
    • list

      <T> List<T> list(Class<T> type)
      Return the response as a list of beans.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The list of beans the response is converted into.
      Throws:
      HttpException - when the response has error status codes
    • stream

      <T> Stream<T> stream(Class<T> type)
      Return the response as a stream of beans.

      Typically the response is expected to be application/x-json-stream newline delimited json payload.

      Note that for this stream request the response content is not deemed 'loggable' by avaje-http-client. This is because the entire response may not be available at the time of the callback. As such RequestLogger will not include response content when logging stream request/response

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Type Parameters:
      T - The type that the content is converted to.
      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The stream of beans from the response
      Throws:
      HttpException - when the response has error status codes
    • bean

      <T> T bean(Type type)
      Return the response as a single bean.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The bean the response is converted into.
      Throws:
      HttpException - when the response has error status codes
    • list

      <T> List<T> list(Type type)
      Return the response as a list of beans.

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The list of beans the response is converted into.
      Throws:
      HttpException - when the response has error status codes
    • stream

      <T> Stream<T> stream(Type type)
      Return the response as a stream of beans.

      Typically the response is expected to be application/x-json-stream newline delimited json payload.

      Note that for this stream request the response content is not deemed 'loggable' by avaje-http-client. This is because the entire response may not be available at the time of the callback. As such RequestLogger will not include response content when logging stream request/response

      If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException when using an async request.

      Parameters:
      type - The type of the bean to convert the response content into.
      Returns:
      The stream of beans from the response
      Throws:
      HttpException - when the response has error status codes
    • asVoid

      HttpResponse<Void> asVoid()
      Return the response with check for 200 range status code.

      Will throw an HttpException if the status code is in the error range allowing the caller to access the error message body via HttpException.bean(Class)

      This is intended to be used for POST, PUT, DELETE requests where the caller is only interested in the response body when an error occurs (status code not in 200 range).

      Throws:
      HttpException - when the response has error status codes
    • asDiscarding

      HttpResponse<Void> asDiscarding()
      Return the response discarding the response content.

      Unlike asVoid() this will discard any response body including any error response body. We should instead use asVoid() if we might get an error response body that we want to read via for example HttpException.bean(Class).

    • asString

      HttpResponse<String> asString()
      Return the content as string.
    • asPlainString

      HttpResponse<String> asPlainString()
      Return the content as string with check for 200 range status code.

      If the status code is in the error range then a HttpException is thrown.

    • asInputStream

      HttpResponse<InputStream> asInputStream()
      Return the content as InputStream.
    • asLines

      Return the content as a stream of string lines.
    • asByteArray

      HttpResponse<byte[]> asByteArray()
      Return the content as byte array.
    • asFile

      HttpResponse<Path> asFile(Path file)
      Return the content into the given file.
    • handler

      <T> HttpResponse<T> handler(HttpResponse.BodyHandler<T> responseHandler)
      Return the response using the given response body handler.