Interface HttpClient

All Superinterfaces:
AutoCloseable

public interface HttpClient extends AutoCloseable
The HTTP client context that we use to build and process requests.


   HttpClient client = HttpClient.builder()
       .baseUrl("http://localhost:8080")
       .bodyAdapter(new JacksonBodyAdapter())
       .build();

  HelloDto dto = client.request()
       .path("hello")
       .queryParam("name", "Rob")
       .queryParam("say", "Whats up")
       .GET()
       .bean(HelloDto.class);

 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Builds the HttpClient.
    static interface 
    Components register Generated Client interface Providers
    static interface 
    Statistic metrics collected to provide an overview of activity of this client.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the BodyAdapter that this client is using.
    Return the builder to config and build the client context.
    void
    Note: invoking this method has no effect on JDK versions less than 21.
    <T> T
    create(Class<T> clientInterface)
    Return the http client API implementation.
    Return the current aggregate metrics.
    metrics(boolean reset)
    Return the current metrics with the option of resetting the underlying counters.
    Create a new request.
    url()
    Returns a UrlBuilder to build a URL, considering the base URL.
  • Method Details

    • builder

      static HttpClient.Builder builder()
      Return the builder to config and build the client context.
      
      
         HttpClient client = HttpClient.builder()
             .baseUrl("http://localhost:8080")
             .bodyAdapter(new JacksonBodyAdapter())
             .build();
      
        HttpResponse<String> res = client.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()
      Returns a UrlBuilder to build a URL, considering the base URL.
    • bodyAdapter

      BodyAdapter bodyAdapter()
      Return the BodyAdapter that this client is using.
    • metrics

      Return the current aggregate metrics.

      These metrics are collected for all requests sent via this context.

    • metrics

      HttpClient.Metrics metrics(boolean reset)
      Return the current metrics with the option of resetting the underlying counters.

      These metrics are collected for all requests sent via this context.

    • close

      void close()
      Note: invoking this method has no effect on JDK versions less than 21.

      Initiates an orderly shutdown in which http requests previously submitted are run to completion, but no new requests will be accepted. Running a request to completion may involve running several operations in the background, including waiting for responses to be delivered. This method waits until all operations have completed execution and the client has terminated.

      Specified by:
      close in interface AutoCloseable