Skip to content

[REFACTOR] Standardize pagination response wrapper using PagedResponse<T> #216

Closed

Description

@aniebietafia

Problem

The API handles pagination inconsistently across different features:

  • Some controllers return ResponseEntity<UnravelDocsResponse<Page<T>>> (e.g., AdminController.java, ReceiptController.java).
  • Some controllers return raw Spring Data Page<T> (e.g., PayPalPaymentController.java, StripePaymentController.java, NotificationController.java).

Exposing the raw Spring Data org.springframework.data.domain.Page directly leaks internal framework specifics (such as pageable, sort, numberOfElements) to the public API contract, inflating payload sizes and causing discrepancies when pagination structures serialize differently.

Proposed Solution

Create a reusable, standardized PagedResponse<T> wrapper. Modify all paginated controller endpoints to return ApiResponse<PagedResponse<T>>.

User Stories

  • As an API client, I want all paginated endpoints to structure list content and metadata (page, size, total elements, total pages, etc.) identically so that I can reuse a single paging component in the UI.
  • As a developer, I want to decouple the database pagination framework from the public API schema.

Acceptance Criteria

  • PagedResponse<T> class is defined under src/main/java/com/xtraction/api/shared/response/PagedResponse.java.
  • Raw Spring Data Page objects are mapped to PagedResponse<T> using a utility mapper.
  • All controllers returning paginated content return ApiResponse<PagedResponse<T>>.
  • Internal Spring pagination details (e.g., sort, pageable) are stripped from public responses.

Proposed Technical Details

package com.xtraction.api.shared.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PagedResponse<T> {
    private List<T> content;
    private int page;
    private int size;
    private long totalElements;
    private int totalPages;
    private boolean last;
    private boolean first;

    public static <T> PagedResponse<T> fromPage(Page<T> page) {
        return PagedResponse.<T>builder()
                .content(page.getContent())
                .page(page.getNumber())
                .size(page.getSize())
                .totalElements(page.getTotalElements())
                .totalPages(page.getTotalPages())
                .last(page.isLast())
                .first(page.isFirst())
                .build();
    }
}

Tasks

  • Define the PagedResponse class.
  • Audit and identify all controllers utilizing Page<T> or Slice<T>.
  • Refactor those endpoints to map database pages to PagedResponse payloads.
  • Ensure that Swagger specifications show ApiResponsePagedResponse correctly.

Activity

  1. added
    enhancement
    on Jun 20, 2026
  2. self-assigned this
    on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

apicidatabaseemailenhancementpayment

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions