Home » Java » java – Make Spring Boot REST API to provide paged Response for External API-Exceptionshub

java – Make Spring Boot REST API to provide paged Response for External API-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was searching for this kind of example from long time.
I have read about JPA paging examples, but for me, the case is different here.

There is a need for my project to Create SpringBoot REST API, which calls internally some external API (JSON based) using the REST Template.
Now once received the response, the response from my SpringBoot service should go as the first page and not all data.
There is also a need for some caching mechanism here.
But for any new call to my Service should not have a call to External Service again, it should a response from cached data, as the next page.
There can be some mechanism as a daily cache for the data, but this can be changed as per need basis.

An important thing I am looking for some example of the solution as How to perform Page based response of REST API in Spring boot, on Response received from External API through REST-Template.

How to&Answers:

I would suggest implementing the pagination mechanism at the external API and passing the page details as part of the query parameter.

@GetMapping("/user")
User getDetails(Pageable page){
...
} 

Also provide a Link header with details of next, previous, last and first page details in the response headers from the external apis

Link: <https://abc/user?page=2&size=100>; rel="next",
 <https://abc/user?page=200&size=100>; rel="last",
 <https://abc/user?page=1&size=100>; rel="prev",
 <https://abc/user?page=1&size=100>; rel="first"

Have a Redis cache between your database and data access layer but only for high frequency read queries with least frequent updates.