Interface HttpClientContext


public interface HttpClientContext
The HTTP client context that we use to build and process requests.


   HttpClientContext ctx = HttpClientContext.newBuilder()
       .withBaseUrl("http://localhost:8080")
       .withBodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
       .build();

  HelloDto dto = ctx.request()
       .path("hello")
       .queryParam("name", "Rob")
       .queryParam("say", "Ki ora")
       .get()
       .bean(HelloDto.class);

 
  • Method Details

    • newBuilder

      static HttpClientContext.Builder newBuilder()
      Return the builder to config and build the client context.
      
      
         HttpClientContext ctx = HttpClientContext.newBuilder()
             .withBaseUrl("http://localhost:8080")
             .withBodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
             .build();
      
        HttpResponse<String> res = ctx.request()
             .path("hello")
             .get().asString();
      
       
    • create

      <T> T create​(Class<T> clientInterface)
      Return the http client API implementation.
      Type Parameters:
      T - The service type.
      Parameters:
      clientInterface - A @Client interface with annotated API methods.
      Returns:
      The http client API implementation.
    • request

      Create a new request.
    • url

      UrlBuilder url()
      Return a UrlBuilder to use to build an URL taking into account the base URL.
    • converters

      BodyAdapter converters()
      Return the body adapter used by the client context.

      This is the body adapter used to convert request and response bodies to java types. For example using Jackson with JSON payloads.

    • httpClient

      HttpClient httpClient()
      Return the underlying http client.
    • checkResponse

      void checkResponse​(HttpResponse<?> response)
      Check the response status code and throw HttpException if the status code is in the error range.
    • readContent

      BodyContent readContent​(HttpResponse<byte[]> httpResponse)
      Return the response content taking into account content encoding.
      Parameters:
      httpResponse - The HTTP response to decode the content from
      Returns:
      The decoded content
    • decodeContent

      byte[] decodeContent​(HttpResponse<byte[]> httpResponse)
      Decode the response content given the Content-Encoding http header.
      Parameters:
      httpResponse - The HTTP response
      Returns:
      The decoded content
    • decodeContent

      byte[] decodeContent​(String encoding, byte[] content)
      Decode the body using the given encoding.
      Parameters:
      encoding - The encoding used to decode the content
      content - The raw content being decoded
      Returns:
      The decoded content