I am using spring boot with hibernate and I want to use pagination in my project. I have searched on google and saw many examples but I am unable to implement it in my proje
I would consider using org.springframework.data.domain.Pageable
directly into your controller. This object can then be passed to your JPA layer where it will handle the number of returned results and the size.
The great thing about using Pageable
is that it returns a Page
object which can be used on the front-end to form previous/next page logic.
By default this class uses url parameters 'page' and 'size'; hence page=0&size=10 will return the first 10 items.
Hence in your case the code could look something like:
@ResponseBody
@RequestMapping("/top/pages/")
public List getAllPosts(@PageableDefault(value=10, page=0) Pageable pageable) throws ServletException {
Page page = postDao.findAll(pageable);
return page.getContent();
}
Notice the annotation @PageableDefault
is just to set up the defaults & it's not required.
In the front-end the next page call can be Next
; this will return a list of Posts from 11 to 20.