Class ApiResponse<T>

java.lang.Object
io.github.og4dev.dto.ApiResponse<T>
Type Parameters:
T - the type of the response content (can be any Java type or Void for no content)

public class ApiResponse<T> extends Object
Standard API Response wrapper for Spring Boot applications.

This class provides a consistent, type-safe structure for API responses across your application, including HTTP status codes, descriptive messages, content payload, and automatic timestamps. It supports both successful and error responses with optional content, ensuring a uniform API contract.

The response structure follows a standardized format:

  • status - HTTP status code (200, 201, 404, etc.)
  • message - Human-readable description of the response
  • content - The response payload (generic type T, optional)
  • timestamp - RFC 3339 UTC timestamp (auto-generated)

Thread Safety: This class is immutable and thread-safe. All fields are final and set during construction. The response object can be safely shared across threads without synchronization.

Usage Examples:


 // Success response with data
 return ApiResponse.success("User retrieved successfully", user);

 // Created response (HTTP 201)
 return ApiResponse.created("User created successfully", newUser);

 // Success response without data
 return ApiResponse.success("User deleted successfully");

 // Custom status response
 return ApiResponse.status("Request accepted", HttpStatus.ACCEPTED);
 

JSON Serialization: The class uses Jackson's @JsonInclude(NON_NULL) to exclude null fields from the JSON output, reducing response payload size.

Since:
1.0.0
Version:
1.1.0
Author:
Pasindu OG
See Also:
  • ResponseEntity
  • HttpStatus
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder class for constructing ApiResponse instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> org.springframework.http.ResponseEntity<ApiResponse<T>>
    created(String message, T content)
    Creates a CREATED (201) response with a message and content.
    Gets the response content.
    Gets the response message.
    Gets the HTTP status code.
    Gets the response timestamp.
    static org.springframework.http.ResponseEntity<ApiResponse<Void>>
    status(String message, org.springframework.http.HttpStatus status)
    Creates a response with a custom HTTP status and message only.
    static <T> org.springframework.http.ResponseEntity<ApiResponse<T>>
    status(String message, T content, org.springframework.http.HttpStatus status)
    Creates a response with a custom HTTP status, message, and content.
    static org.springframework.http.ResponseEntity<ApiResponse<Void>>
    success(String message)
    Creates a SUCCESS (200) response with only a message.
    static <T> org.springframework.http.ResponseEntity<ApiResponse<T>>
    success(String message, T content)
    Creates a SUCCESS (200) response with a message and content.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getStatus

      public Integer getStatus()
      Gets the HTTP status code.
      Returns:
      the status code
    • getMessage

      public String getMessage()
      Gets the response message.
      Returns:
      the message
    • getContent

      public T getContent()
      Gets the response content.
      Returns:
      the content
    • getTimestamp

      public Instant getTimestamp()
      Gets the response timestamp.
      Returns:
      the timestamp
    • created

      public static <T> org.springframework.http.ResponseEntity<ApiResponse<T>> created(String message, T content)
      Creates a CREATED (201) response with a message and content.
      Type Parameters:
      T - the type of the response content
      Parameters:
      message - the response message
      content - the response content
      Returns:
      a ResponseEntity with CREATED status
    • success

      public static org.springframework.http.ResponseEntity<ApiResponse<Void>> success(String message)
      Creates a SUCCESS (200) response with only a message.
      Parameters:
      message - the response message
      Returns:
      a ResponseEntity with OK status
    • success

      public static <T> org.springframework.http.ResponseEntity<ApiResponse<T>> success(String message, T content)
      Creates a SUCCESS (200) response with a message and content.
      Type Parameters:
      T - the type of the response content
      Parameters:
      message - the response message
      content - the response content
      Returns:
      a ResponseEntity with OK status
    • status

      public static org.springframework.http.ResponseEntity<ApiResponse<Void>> status(String message, org.springframework.http.HttpStatus status)
      Creates a response with a custom HTTP status and message only.
      Parameters:
      message - the response message
      status - the HTTP status
      Returns:
      a ResponseEntity with the specified status
    • status

      public static <T> org.springframework.http.ResponseEntity<ApiResponse<T>> status(String message, T content, org.springframework.http.HttpStatus status)
      Creates a response with a custom HTTP status, message, and content.
      Type Parameters:
      T - the type of the response content
      Parameters:
      message - the response message
      content - the response content
      status - the HTTP status
      Returns:
      a ResponseEntity with the specified status