Interface RestClient.Builder

Enclosing interface:
RestClient

public static interface RestClient.Builder
A mutable builder for creating a RestClient.
  • Method Details

    • baseUrl

      RestClient.Builder baseUrl(String baseUrl)
      Configure a base URL for requests. Effectively a shortcut for:
       String baseUrl = "https://abc.go.com/v1";
       DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
       RestClient client = RestClient.builder().uriBuilderFactory(factory).build();
       

      The DefaultUriBuilderFactory is used to prepare the URL for every request with the given base URL, unless the URL request for a given URL is absolute in which case the base URL is ignored.

      Note: this method is mutually exclusive with uriBuilderFactory(UriBuilderFactory). If both are used, the baseUrl value provided here will be ignored.

      Returns:
      this builder
      See Also:
    • baseUrl

      RestClient.Builder baseUrl(URI baseUrl)
      Configure a base URI for requests. Effectively a shortcut for:
       URI baseUrl = URI.create("https://abc.go.com/v1");
       DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl.toString());
       RestClient client = RestClient.builder().uriBuilderFactory(factory).build();
       

      The DefaultUriBuilderFactory is used to prepare the URL for every request with the given base URL, unless the URL request for a given URL is absolute in which case the base URL is ignored.

      Note: this method is mutually exclusive with uriBuilderFactory(UriBuilderFactory). If both are used, the baseUrl value provided here will be ignored.

      Returns:
      this builder
      Since:
      6.2
      See Also:
    • defaultUriVariables

      RestClient.Builder defaultUriVariables(Map<String,?> defaultUriVariables)
      Configure default URL variable values to use when expanding URI templates with a Map. Effectively a shortcut for:
       Map<String, ?> defaultVars = ...;
       DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
       factory.setDefaultVariables(defaultVars);
       RestClient client = RestClient.builder().uriBuilderFactory(factory).build();
       

      Note: this method is mutually exclusive with uriBuilderFactory(UriBuilderFactory). If both are used, the defaultUriVariables value provided here will be ignored.

      Returns:
      this builder
      See Also:
    • uriBuilderFactory

      RestClient.Builder uriBuilderFactory(UriBuilderFactory uriBuilderFactory)
      Provide a pre-configured UriBuilderFactory instance. This is an alternative to, and effectively overrides the following shortcut properties:
      Parameters:
      uriBuilderFactory - the URI builder factory to use
      Returns:
      this builder
      See Also:
    • defaultHeader

      RestClient.Builder defaultHeader(String header, String... values)
      Global option to specify a header to be added to every request, if the request does not already contain such a header.
      Parameters:
      header - the header name
      values - the header values
      Returns:
      this builder
    • defaultHeaders

      RestClient.Builder defaultHeaders(Consumer<HttpHeaders> headersConsumer)
      Provide a consumer to access to every default header declared so far, with the possibility to add, replace, or remove.
      Parameters:
      headersConsumer - the consumer
      Returns:
      this builder
    • defaultRequest

      RestClient.Builder defaultRequest(Consumer<RestClient.RequestHeadersSpec<?>> defaultRequest)
      Provide a consumer to customize every request being built.
      Parameters:
      defaultRequest - the consumer to use for modifying requests
      Returns:
      this builder
    • defaultStatusHandler

      RestClient.Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate, RestClient.ResponseSpec.ErrorHandler errorHandler)
      Register a default status handler to apply to every response. Such default handlers are applied in the order in which they are registered, and after any others that are registered for a specific response.
      Parameters:
      statusPredicate - to match responses with
      errorHandler - handler that typically, though not necessarily, throws an exception
      Returns:
      this builder
    • defaultStatusHandler

      RestClient.Builder defaultStatusHandler(ResponseErrorHandler errorHandler)
      Register a default status handler to apply to every response. Such default handlers are applied in the order in which they are registered, and after any others that are registered for a specific response.
      Parameters:
      errorHandler - handler that typically, though not necessarily, throws an exception
      Returns:
      this builder
    • requestInterceptor

      RestClient.Builder requestInterceptor(ClientHttpRequestInterceptor interceptor)
      Add the given request interceptor to the end of the interceptor chain.
      Parameters:
      interceptor - the interceptor to be added to the chain
      Returns:
      this builder
    • requestInterceptors

      RestClient.Builder requestInterceptors(Consumer<List<ClientHttpRequestInterceptor>> interceptorsConsumer)
      Manipulate the interceptors with the given consumer. The list provided to the consumer is "live", so that the consumer can be used to remove interceptors, change ordering, etc.
      Parameters:
      interceptorsConsumer - a function that consumes the interceptors list
      Returns:
      this builder
    • requestInitializer

      RestClient.Builder requestInitializer(ClientHttpRequestInitializer initializer)
      Add the given request initializer to the end of the initializer chain.
      Parameters:
      initializer - the initializer to be added to the chain
      Returns:
      this builder
    • requestInitializers

      RestClient.Builder requestInitializers(Consumer<List<ClientHttpRequestInitializer>> initializersConsumer)
      Manipulate the initializers with the given consumer. The list provided to the consumer is "live", so that the consumer can be used to remove initializers, change ordering, etc.
      Parameters:
      initializersConsumer - a function that consumes the initializers list
      Returns:
      this builder
    • requestFactory

      RestClient.Builder requestFactory(ClientHttpRequestFactory requestFactory)
      Configure the ClientHttpRequestFactory to use. This is useful for plugging in and/or customizing options of the underlying HTTP client library (e.g. SSL).

      If no request factory is specified, RestClient uses Apache Http Client, Jetty Http Client if available on the classpath, and defaults to the JDK HttpClient if the java.net.http module is loaded, or to a simple default otherwise.

      Parameters:
      requestFactory - the request factory to use
      Returns:
      this builder
    • messageConverters

      RestClient.Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer)
      Configure the message converters for the RestClient to use.
      Parameters:
      configurer - the configurer to apply
      Returns:
      this builder
    • observationRegistry

      RestClient.Builder observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
      Configure the ObservationRegistry to use for recording HTTP client observations.
      Parameters:
      observationRegistry - the observation registry to use
      Returns:
      this builder
    • observationConvention

      RestClient.Builder observationConvention(ClientRequestObservationConvention observationConvention)
      Configure the ObservationConvention to use for collecting metadata for the request observation. Will use DefaultClientRequestObservationConvention if none provided.
      Parameters:
      observationConvention - the observation convention to use
      Returns:
      this builder
    • apply

      Apply the given Consumer to this builder instance.

      This can be useful for applying pre-packaged customizations.

      Parameters:
      builderConsumer - the consumer to apply
      Returns:
      this builder
    • clone

      Clone this RestClient.Builder.
    • build

      RestClient build()
      Build the RestClient instance.