Module bus.http

Class RequestBuilder<T extends RequestBuilder>

java.lang.Object
org.miaixz.bus.http.plugin.httpz.RequestBuilder<T>
Type Parameters:
T - The type of the concrete builder implementation, for method chaining.
Direct Known Subclasses:
DeleteBuilder, GetBuilder, HeadBuilder, PostBuilder, PutBuilder

public abstract class RequestBuilder<T extends RequestBuilder> extends Object
An abstract base builder for creating HTTP requests. It provides a fluent API for setting common request properties like URL, headers, parameters, and tags.
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • httpd

      protected Httpd httpd
      The HTTP client instance.
    • id

      protected String id
      A unique identifier for the request.
    • url

      protected String url
      The URL for the request.
    • tag

      protected Object tag
      An optional tag for the request, used for cancellation.
    • headers

      protected Map<String,String> headers
      A map of request headers.
    • params

      protected Map<String,String> params
      A map of request parameters (e.g., query string or form data).
    • encoded

      protected Map<String,String> encoded
      A map of pre-encoded request parameters.
  • Constructor Details

    • RequestBuilder

      public RequestBuilder(Httpd httpd)
      Constructs a new RequestBuilder.
      Parameters:
      httpd - The Httpd client instance.
  • Method Details

    • id

      public T id(String id)
      Sets the unique identifier for this request.
      Parameters:
      id - The request ID.
      Returns:
      This builder instance for chaining.
    • url

      public T url(String url)
      Sets the URL for this request.
      Parameters:
      url - The request URL.
      Returns:
      This builder instance for chaining.
    • tag

      public T tag(Object tag)
      Sets the tag for this request, which can be used to cancel it later.
      Parameters:
      tag - The request tag.
      Returns:
      This builder instance for chaining.
    • addHeader

      public T addHeader(Map<String,String> headers)
      Replaces all existing headers with the given map of headers.
      Parameters:
      headers - A map of headers to set.
      Returns:
      This builder instance for chaining.
    • addHeader

      public T addHeader(String key, String val)
      Adds a single header to the request.
      Parameters:
      key - The header name.
      val - The header value.
      Returns:
      This builder instance for chaining.
    • addParam

      public T addParam(Map<String,String> params)
      Replaces all existing parameters with the given map of parameters.
      Parameters:
      params - A map of parameters to set.
      Returns:
      This builder instance for chaining.
    • addParam

      public T addParam(String key, String val)
      Adds a single parameter to the request.
      Parameters:
      key - The parameter name.
      val - The parameter value.
      Returns:
      This builder instance for chaining.
    • addParam

      public T addParam(Object object)
      Adds all properties of a Plain Old Java Object (POJO) as parameters.
      Parameters:
      object - The object whose properties will be added as parameters.
      Returns:
      This builder instance for chaining.
    • addEncoded

      public T addEncoded(Map<String,String> params)
      Replaces all existing pre-encoded parameters with the given map.
      Parameters:
      params - A map of pre-encoded parameters to set.
      Returns:
      This builder instance for chaining.
    • addEncoded

      public T addEncoded(String key, String val)
      Adds a single pre-encoded parameter to the request.
      Parameters:
      key - The pre-encoded parameter name.
      val - The pre-encoded parameter value.
      Returns:
      This builder instance for chaining.
    • build

      public abstract RequestCall build()
      Abstract method to be implemented by subclasses to build the final, executable request call.
      Returns:
      The constructed RequestCall.