'Is there a way to make a lot of RestTemplate calls to a paginated endpoint automatically to fetch all the pages

Suppose we have a paginated API which returns some data and also page/perPage/pages/totalCount values. We need to retrieve all the pages in a loop on a client side using Spring RestTemplate.

A simple way would be making one single call with page=1, then getting pages value (or calculate it if it's not exist in a way like totalCount/perPage), and finally, performing several calls with page=2..pages.

Is there a way to do it automatically? I can imagine, e.g., passing Pageable somehow to the one of RestTemplate overloaded methods but it seems there is no appropriate method for that. Maybe some other solutions exist? I'm open to using any of 3rd party libraries if needed.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source