Class RequestSpec<Req,​Res>

  • Type Parameters:
    Req - the type of the request body (use Void for requests without body)
    Res - the type of the expected response

    public class RequestSpec<Req,​Res>
    extends Object
    Immutable specification of an HTTP request containing all parameters needed for execution.

    A RequestSpec encapsulates the complete definition of an API request including the endpoint, HTTP method, query parameters, headers, request body, and expected response type. Instances are created using the RequestBuilder fluent API.

    Request Components

    • Endpoint - The API path (relative or absolute URL)
    • Method - HTTP method (GET, POST, PUT, DELETE, etc.)
    • Query Parameters - URL query string parameters
    • Headers - HTTP request headers via RocketHeaders
    • Body - Request payload (for POST, PUT, PATCH)
    • Response Type - Expected Java class for response deserialization

    Creating Request Specifications

    
     // Simple GET request
     RequestSpec<Void, User> getUser = RequestBuilder.<Void, User>get("/users/1")
         .responseType(User.class)
         .build();
    
     // POST request with body
     CreateUserRequest body = new CreateUserRequest("John", "john@example.com");
     RequestSpec<CreateUserRequest, User> createUser = RequestBuilder
         .<CreateUserRequest, User>post("/users")
         .body(body)
         .responseType(User.class)
         .build();
    
     // GET with query parameters
     Map<String, String> params = new HashMap<>();
     params.put("page", "1");
     params.put("limit", "20");
     RequestSpec<Void, UserList> listUsers = RequestBuilder.<Void, UserList>get("/users")
         .queryParams(params)
         .responseType(UserList.class)
         .build();
     

    Executing Requests

    
     // With synchronous client
     User user = client.sync().execute(getUser);
    
     // With async client
     CompletableFuture<User> future = client.async().execute(getUser);
    
     // With fluent client
     Result<User, ApiError> result = client.fluent().execute(getUser);
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    RequestBuilder, RocketHeaders