Class DefaultHttpClient

  • All Implemented Interfaces:
    RocketClient

    public class DefaultHttpClient
    extends Object
    implements RocketClient
    Default implementation of RocketClient using Java's built-in HttpURLConnection.

    This client provides HTTP request execution without external dependencies, making it suitable for environments where third-party HTTP libraries cannot be used. It supports all standard HTTP methods, custom headers, SSL/TLS configuration, and JSON serialization.

    Features

    • Zero external HTTP dependencies (uses java.net.HttpURLConnection)
    • Automatic JSON serialization/deserialization via Jackson
    • SSL/TLS support with custom certificate configuration
    • Configurable timeouts and request options
    • Query parameter encoding and URL building
    • Comprehensive error handling with status codes

    Direct Usage

    
     // Create a client
     DefaultHttpClient client = new DefaultHttpClient("https://api.example.com");
    
     // Build and execute a request
     RequestSpec<Void, User> request = RequestBuilder.<Void, User>get("/users/1")
         .responseType(User.class)
         .build();
    
     User user = client.execute(request);
     

    With Custom Options

    
     RocketRestOptions options = new RocketRestOptions();
     options.set(RocketRestOptions.LOGGING_ENABLED, true);
     options.set(RocketRestOptions.LOG_RESPONSE_BODY, true);
    
     DefaultHttpClient client = new DefaultHttpClient(
         "https://api.example.com",
         options
     );
     

    Note

    For most use cases, prefer using RocketRest facade or RocketClientFactory instead of instantiating this class directly.

    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    RocketClient, RocketClientFactory, RocketRest
    • Constructor Detail

      • DefaultHttpClient

        public DefaultHttpClient​(String baseUrl)
        Creates a new DefaultHttpClient with the specified base URL.
        Parameters:
        baseUrl - The base URL for all requests
      • DefaultHttpClient

        public DefaultHttpClient​(String baseUrl,
                                 RocketRestOptions clientOptions)
        Creates a new DefaultHttpClient with the specified base URL and client options.
        Parameters:
        baseUrl - The base URL for all requests
        clientOptions - The client options
    • Method Detail

      • configureSsl

        public void configureSsl​(SSLContext sslContext)
        Description copied from interface: RocketClient
        Sets the SSL context to be used for HTTPS requests.
        Specified by:
        configureSsl in interface RocketClient
        Parameters:
        sslContext - The SSL context to use.
      • getClientOptions

        public RocketRestOptions getClientOptions()
        Gets the client options.
        Returns:
        The client options
      • execute

        public <Req,​Res> Res execute​(RequestSpec<Req,​Res> requestSpec)
                                    throws RocketRestException
        Description copied from interface: RocketClient
        Executes an HTTP request based on the provided request specification.
        Specified by:
        execute in interface RocketClient
        Type Parameters:
        Req - The type of the request body.
        Res - The type of the response.
        Parameters:
        requestSpec - The specification of the request to be executed.
        Returns:
        The response object.
        Throws:
        RocketRestException - If an error occurs during the request execution.