Open
Description
Currently, PagedModel<T>
directly holds a Page<T>
instance, which introduces unnecessary complexity during JSON serialization and deserialization.
Therefore, it is suggested to remove the Page<T>
attribute and leave only content and pageMetaData
Proposed Solution
- Remove the direct dependency on Page and instead manage only the necessary data.
- Simplify the internal structure by keeping only List content and PageMetadata page.
- Ensure proper deserialization support using a @JsonCreator constructor.
// Partial implementation highlighting the proposed changes
public class PagedModel<T> {
private final List<T> content;
private final PageMetadata page;
public PagedModel(Page<T> page) {
Assert.notNull(page, "Page must not be null");
this.page = new PageMetadata(page.getSize(), page.getNumber(),
page.getTotalElements(), page.getTotalPages());
this.content = page.getContent();
}
@JsonCreator
private PagedModel(
@JsonProperty("content") List<T> content,
@JsonProperty("page") PageMetadata metadata) {
this.content = content;
this.page = metadata;
}
}
Improved Usability in Tests
By applying this change, writing test cases for API responses becomes more convenient, especially when using WebTestClient in a Spring Boot environment.
val result = webTestClient
.get()
.uri("/api/v1/test")
.exchange()
.expectStatus().isOk
.expectBody(object : ParameterizedTypeReference<PagedModel<TestData>>() {})
.returnResult()
result.responseBody.shouldNotBeNull()
result.responseBody.data?.content?.size shouldBe 5