Spring WebClient: Call Rest Service which has paginated response

Viewed 22

I want to hit a service which has a paginated response using Web Client. ie. I hit a service, check if it returns hasMoreElements as TRUE, then call the service again with updated request parameters like START_ROW, END_ROW, PAGE_NUMBER. What is the best approach to achieve this? Currently am just looping through the results and hitting the service again. But their should be a better approach to this. PFB my pseudocode. Any libraries I can use?

boolean hasMoreElements=true;

while(!hasMoreElements==false)
{
response=webClient.post().header(HEADERS).bodyValue(REQUEST).block();
Get the NEW START ROW, END ROW, AND PAGE NUMBER and SET in the REQUEST
Get the hasMoreElements value
}
1 Answers

Use JPARepository with paging for this.

You can return a list of objects and check if its length is less then the limit passed, if yes then you can stop fetching.

You could also return a Page or a Slice instead which gives you a little bit more information about the current and next fetch cycle

Related