1-1. 파라미터를 Pageable 객체로 받기
@GetMapping("/api/posts")
public Page<Post> getPosts(@RequestParam String category ,
Pageable pageable) {
...생략
}
@RequestParam
없이 받아야한다.@GetMapping("/api/posts")
public Page<Post> getPosts(@RequestParam String sort,
@RequestParam String category ,
Pageable pageable) {
...생략
}
Page request [number: 0, size 5, sort: default: ASC]
Page request [number: 0, size 5, sort: UNSORTED]
Page request [number: 0, size 5, sort: createdAt: ASC]
?category=ALL&page=0&size=5&sort=CREATED_AT,DESC
Page request [number: 0, size 5, sort: CREATED_AT: DESC]
1-2. 파라미터를 sort, page, size를 따로 받기.
@GetMapping("/api/posts")
public Page<GetPostsResponseDto> getPosts(@RequestParam String sort,
@RequestParam String category ,
@RequestParam int page,
@RequestParam int size) {
return postService.getPosts(sort, category, page, size);
}
public Page<GetPostsResponseDto> getPosts(@RequestParam String sort,
@RequestParam String category,
@RequestParam int page,
@RequestParam int size) {
Pageable pageable = PageRequest.of(page,size);
return postRepositoryImpl.findAllByCategoryOrderBySort(sort, category, pageable);
}
Page request [number: 0, size 5, sort: UNSORTED]
@Override
public Page<GetPostsResponseDto> findAllByCategoryOrderBySort(String sort, String category, Pageable pageable) {
List<GetPostsResponseDto> returnPost = queryFactory.select(Projections.fields(
GetPostsResponseDto.class,
post.title,
post.category,
post.deadline,
post.numberPeople,
post.currentNumberPeople,
post.contactMethod,
post.viewCount,
post.user.nickname,
post.imageUrl
))
.from(post)
.where(categoryContains(category))
.orderBy(orderByValidDeadline(),getOrderSpecifier(sort))
.fetch();
return new PageImpl<>(returnPost,pageable,returnPost.size());
}
return new PageImpl<>(returnPost,pageable,returnPost.size());
{
"content": [
{
"title": "amo",
"category": "DELIVERY",
"deadline": 1663243762507,
"numberPeople": 9,
"currentNumberPeople": 4,
"viewCount": 65,
"nickname": "123",
},
{
"title": "dpu",
"category": "EXHIBITION",
"deadline": 1660593585499,
"numberPeople": 4,
"currentNumberPeople": 3,
"viewCount": 40,
"nickname": "123",
},
...
],
"pageable": {
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 5,
"paged": true,
"unpaged": false
},
"last": false,
"totalElements": 99,
"totalPages": 20,
"size": 5,
"number": 0,
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"first": true,
"numberOfElements": 99,
"empty": false
}
Slice<User> findByLastname(String lastname, Pageable pageable);
Page<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Sort sort);
잘보고 갑니다! 레퍼런스도 정성스럽게 작성해주셔서 많은 도움 되었습니다!