Skip to content

[REFACTOR] Unify response envelope into a single ApiResponse<T> #211

Closed

Description

@aniebietafia

Problem

Currently, the codebase uses two distinct and inconsistent response wrappers:

  1. Success responses use UnravelDocsResponse.java (to be renamed to XtractionResponse.java), which has the fields int statusCode, String status (always "success"), String message, and T data.
  2. Error responses use ErrorResponse.java, which has the fields int statusCode, String error, String message, and String errorCode (nullable).

This structural split forces API consumers to implement separate parsing logic based on HTTP status codes. Additionally, success responses lack standard metadata such as request paths, timestamps, or trace IDs, which are vital for front-end analytics and error troubleshooting.

Proposed Solution

Create a unified ApiResponse<T> envelope class that handles both success and failure cases with a consistent schema. Deprecate and remove the separate ErrorResponse and XtractionResponse (formerly UnravelDocsResponse) classes. Update the shared ResponseBuilderService.java and GlobalExceptionHandler.java to construct and return the unified ApiResponse<T> wrapper.

User Stories

  • As an API consumer, I want the API to return a single consistent response envelope structure so that my client-side wrapper or SDK can parse the response header metadata, success status, and payloads uniformly.
  • As a backend developer, I want to use a unified response wrapper so that I don't have to manage different classes for controller outcomes and exception handlers.

Acceptance Criteria

  • A new generic class ApiResponse<T> is created in src/main/java/com/xtraction/api/shared/response/ApiResponse.java.
  • The class ErrorResponse is deleted.
  • The class UnravelDocsResponse (or XtractionResponse) is deleted.
  • Every REST controller endpoint and exception handler method returns ApiResponse<T> wrapped in ResponseEntity<ApiResponse<T>> (or handled via ResponseBodyAdvice).
  • The JSON response structure contains:
    • success (boolean): true for 2xx codes, false otherwise.
    • statusCode (int): HTTP status code.
    • message (String): Human-readable status or error description.
    • data (T): The payload (null on errors/empty responses).
    • errors (List): Field validation details (null if not a validation error).
    • errorCode (String): central error code (null on success).
    • timestamp (Instant): ISO-8601 UTC timestamp.
    • path (String): Request URI path.
    • traceId (String): Unique transaction ID.
  • Swagger API definitions reflect the unified schema.

Proposed Technical Details

package com.xtraction.api.shared.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Instant;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {
    private boolean success;
    private int statusCode;
    private String message;
    private T data;
    private List<ValidationError> errors;
    private String errorCode;
    private Instant timestamp;
    private String path;
    private String traceId;

    @Data
    @AllArgsConstructor
    public static class ValidationError {
        private String field;
        private Object rejectedValue;
        private String message;
    }
}

Tasks

  • Define the ApiResponse class.
  • Remove ErrorResponse.java and UnravelDocsResponse.java.
  • Refactor ResponseBuilderService.java to construct ApiResponse<T>.
  • Search and replace references across all controller files.
  • Update GlobalExceptionHandler.java to return ApiResponse<Void>.
  • Run test suite to verify no compile-time or serialization issues exist.

Open Questions / Considerations

  • Should the success field be a boolean or a string status (e.g. "SUCCESS", "ERROR")? A boolean is standard and easier to parse in modern single-page applications.

Activity

  1. added
    bug
    enhancement
    on Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibugenhancementquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions