/api/posts?pageNo=1&pageSize=5&sortBy=id&sortDir=dec
1. 컨트롤러
@GetMapping
public PostResponse getAllPosts(
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo ,
@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
@RequestParam(value = "sortBy", defaultValue = "id", required = false) String sortBy,
@RequestParam(value = "sortDir", defaultValue = "asc", required = false) String sortDir
){
return postService.getAllPosts(pageNo, pageSize, sortBy, sortDir);
}
2. PostService
//모든 포스트들을 가져오기 (페이지네이션&sorting)
PostResponse getAllPosts(int pageNo, int pageSize, String sortBy, String sortDir);
3. PostServiceImple
@Override
public PostResponse getAllPosts(int pageNo, int pageSize, String sortBy, String sortDir) {
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ?
Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
//Pageable 객체 생성
Pageable pageable = PageRequest.of(pageNo, pageSize, ?);