I'm trying to implement pagination controller in my Springboot application, but I got this error:
The constructor PageRequest(int, int) is undefined) (problem #1)
I add an argument to match pagerequest (int, int, sort)
to fix the problem, but the sort class is protected, so it says that the constructor PageRequest(int, int, Sort)
is not visible (problem #2)
Problem #1:
@GetMapping("/list")
@ResponseBody
public Page<Posts> Pagination(@RequestParam(defaultValue="0") int page) {
return PostsRepository.findAll(new PageRequest(page,4));
}
Problem #2:
@GetMapping("/list")
@ResponseBody
public Page<Posts> Pagination(@RequestParam(defaultValue="0") int page) {
return PostsRepository.findAll(new PageRequest(page,4,null));
}
As suggested by chrylis you can change your controller to use the Pageable argument. if thats not possible create pageable object and using page and size.
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "3") int size
Pageable paging = PageRequest.of(page, size);