I'm building an API that accepts for my "/search" request multiple parameters so that I can filter results with findAll(Example<T> example)
. In the second stage I filter non trivial types ( Date
) to narrow down my results, which works fine.
The problem now arises where I want to implement pagination. Spring data offers the findAll(Example<T> example, Pageable page)
method, which creates a Page<T>
element for me, which is no more applicable to my filtering mechanisms. Though Page<T>
offers a .filter(Predicate p)
function, this converts my Page to a Streamable, which is not what I need for pagination.
How can I further filter a Page<T>
object without ending up with a unintended data format?
I solved the issue by first filtering my result set without the pagination information, and later applying the submitted pagination parameters.
int startIndex = page * size;
int endIndex = (startIndex + size) > myList.size() ? myList.size() : (startIndex + size);
JsonPage<>(myList.subList(startIndex, endIndex), PageRequest.of(page, size), myList.size());
That is way more intuitive than creating the page object and unpack it to get the filterable objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.