Skip to content

Commit 68a8889

Browse files
committed
feat: 페이징 응답 전용 DTO 구현
- 기존 Page 인터페이스의 직렬화 문제로 인해 스프링 부트 3.3부터 추가된 WARNING `Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!` - `@EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO)` 옵션을 적용해 직렬화를 적용할 수 있지만, 응답 데이터가 일부 누락되는 것을 확인 - 직접 페이징 응답 전용 DTO를 구현함으로써 해결
1 parent 8584f34 commit 68a8889

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dmu.dasom.api.global.dto;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import jakarta.validation.constraints.NotEmpty;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
import org.springframework.data.domain.Page;
8+
9+
import java.util.List;
10+
11+
@Getter
12+
@Builder
13+
@Schema(name = "PageResponse", description = "페이징 응답 DTO")
14+
public class PageResponse<T> {
15+
16+
@Schema(description = "데이터")
17+
@NotEmpty
18+
private List<T> content;
19+
20+
@Schema(description = "현재 페이지 번호")
21+
private int number;
22+
23+
@Schema(description = "페이지 크기")
24+
private int size;
25+
26+
@Schema(description = "전체 데이터 개수")
27+
private long totalElements;
28+
29+
@Schema(description = "전체 페이지 개수")
30+
private int totalPages;
31+
32+
@Schema(description = "첫 페이지 여부")
33+
private boolean first;
34+
35+
@Schema(description = "마지막 페이지 여부")
36+
private boolean last;
37+
38+
public static <T> PageResponse<T> from(Page<T> page) {
39+
return PageResponse.<T>builder()
40+
.content(page.getContent())
41+
.number(page.getNumber())
42+
.size(page.getSize())
43+
.totalElements(page.getTotalElements())
44+
.totalPages(page.getTotalPages())
45+
.first(page.isFirst())
46+
.last(page.isLast())
47+
.build();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)