Copy @ RestController
@ RequiredArgsConstructor
public class MemberController {
...
@ GetMapping ( "/members" )
public Page < Member > list ( Pageable pageable) {
Page < Member > page = memberRepository . findAll (pageable);
return page;
}
}
Copy {
"content" : [
{
"createdDate" : "2022-05-22T11:58:18.047317" ,
"lastModifiedDate" : "2022-05-22T11:58:18.047317" ,
"createdBy" : "dc7d4eee-f366-4bcf-ab64-47b87f084771" ,
"lastModifiedBy" : "dc7d4eee-f366-4bcf-ab64-47b87f084771" ,
"id" : 1 ,
"username" : "user0" ,
"age" : 0 ,
"team" : null
} ,
{
"createdDate" : "2022-05-22T11:58:18.078782" ,
"lastModifiedDate" : "2022-05-22T11:58:18.078782" ,
"createdBy" : "3aea9092-a323-452a-ad4c-a0d405842ba5" ,
"lastModifiedBy" : "3aea9092-a323-452a-ad4c-a0d405842ba5" ,
"id" : 2 ,
"username" : "user1" ,
"age" : 1 ,
"team" : null
} ,
...
{
"createdDate" : "2022-05-22T11:58:18.121901" ,
"lastModifiedDate" : "2022-05-22T11:58:18.121901" ,
"createdBy" : "51da627f-8564-48a1-bfdb-33b96ca6fd25" ,
"lastModifiedBy" : "51da627f-8564-48a1-bfdb-33b96ca6fd25" ,
"id" : 20 ,
"username" : "user19" ,
"age" : 19 ,
"team" : null
}
] ,
"pageable" : {
"sort" : {
"sorted" : false ,
"unsorted" : true ,
"empty" : true
} ,
"pageNumber" : 0 ,
"pageSize" : 20 ,
"offset" : 0 ,
"paged" : true ,
"unpaged" : false
} ,
"totalPages" : 5 ,
"totalElements" : 100 ,
"last" : false ,
"numberOfElements" : 20 ,
"first" : true ,
"size" : 20 ,
"number" : 0 ,
"sort" : {
"sorted" : false ,
"unsorted" : true ,
"empty" : true
} ,
"empty" : false
}
Copy /members?page=0&size=3&sort=id,desc&sort=username,desc
Copy # 기본 페이지 사이즈
spring.data.web.pageable.default-page-size=20
# 최대 페이지 사이즈
spring.data.web.pageable.max-page-size=2000
Copy @ RestController
@ RequiredArgsConstructor
public class MemberController {
...
@ RequestMapping (value = "/members_page" , method = RequestMethod . GET )
public String list (
@ PageableDefault (
size = 12 ,
sort = "username" ,
direction = Sort . Direction . DESC )
Pageable pageable) {
...
}
}
Copy @ RestController
@ RequiredArgsConstructor
public class MemberController {
...
@ RequestMapping (value = "/members_page" , method = RequestMethod . GET )
public String list (
@ Qualifier ( "member" ) Pageable memberPageable ,
@ Qualifier ( "order" ) Pageable orderPageable) {
...
}
}
Copy /members?member_page=0&order_page=1
MemberDto.java MemberController.java
Copy @ Data
public class MemberDto {
private Long id;
private String username;
public MemberDto ( Member m) {
this . id = m . getId ();
this . username = m . getUsername ();
}
}
Copy @ RestController
@ RequiredArgsConstructor
public class MemberController {
private final MemberRepository memberRepository;
@ GetMapping ( "/members" )
public Page < MemberDto > listPageMap1 ( Pageable pageable) {
Page < Member > page = memberRepository . findAll (pageable);
Page < MemberDto > pageDto = page . map (MemberDto ::new );
return pageDto;
}
// Page.map() 최적화
@ GetMapping ( "/members" )
public Page < MemberDto > listPageMap2 ( Pageable pageable) {
return memberRepository . findAll (pageable) . map (MemberDto ::new );
}
}
Copy spring.data.web.pageable.one-indexed-parameters=true
Copy http://localhost:8080/members?page=1
Copy {
"content" : [
...
] ,
"pageable" : {
"offset" : 0 ,
"pageSize" : 10 ,
// 0 인덱스
"pageNumber" : 0
} ,
// 0 인덱스
"number" : 0 ,
"empty" : false
}