Consider 2 scenarios-
- I want data from spring as paging and sorting then
i) Create a sort object of (org.springframework.data.domain.Sort)
Sort sort = sortType.equals("ASC") ? Sort.by(sortField).ascending() :
Sort.by(sortField).descending();
ii) Pass this sort type to a pageable object
Pageable pageable = PageRequest.of(pageId, pageSize, sort);
iii) Pass this pageable object to JPArepository method
yourRepository.findAll(pageable);
This will return data with pagination and sorting.
Case 2 - Your scenario - From a collection object I want data with pagination
then you have to take the help from PageImpl class.
which offer 2 Constructor to do this
PageImpl(List<T> content, Pageable pageable, long total)
where
- content – the content of this page(Your collection object).
- pageable – the paging information
- total – the total amount of items available.
There is also another constructor
PageImpl(List<T> content)
Note - This will result in the created Page being identical to the entire List.