I have a List
of entities.
How do I convert it to Page
Object using Spring MVC 4 and Spring Data JPA?
There is a Page implementation for that:
final Page<Something> page = new PageImpl<>(theListOfSomething);
There is one more Constructor :
Page<FOO> page = new PageImpl<>(listOfsomething, pageable, listOfsomething.size());
I think you will need to fetch the correct page content as well.
PageRequest pageRequest = PageRequest.of(offset, limit);
List<Product> products = getProducts();
int total = products.size();
int start = toIntExact(pageRequest.getOffset());
int end = Math.min((start + pageRequest.getPageSize()), total);
List<Product> output = new ArrayList<>();
if (start <= end) {
output = products.subList(start, end);
}
return new PageImpl<>(
output,
pageRequest,
total
);