I want to create a REST compliant endpoint in Spring. The endpoint is supposed to allow pagination.
The request is quite simple, GET to url/resource?page=1&pageSize=20. The problem is the request.
I have found 2 big ways of returning the data:
- Return a custom object containing:
{
"page": 1,
"pageSize": 20,
"content": [
{...},
{...}
...
]
}
The problem with this method is that it no longer returns a resource, it returns a wrapper to the resource.
- Set X-Total-Count custom header when you give the response from backend:
[
{...},
{...}
...
]
This resolves the resource issue, but it makes the endpoint harder to use, which is counter-intuitive since it requires extra work to implement as well.
I wanted to know, what standards are more widely used and why? What is the "new" approach to this? What should I stay away from? Is there a way to satisfy both needs?
Thanks in advance!