D
D
Dmitry2018-10-28 18:51:53
RESTful API
Dmitry, 2018-10-28 18:51:53

How to properly organize rest api pagination?

Hello.
According to the 'commandments' of building a rest api, I must answer, if this is a list of entities, add information on the limit, offset and the total number of records;

{
 "listInfo" : {
  "limit": 20,
  "offset": 0,
  "count": 150
}, 
"items": [
   ...
]
}

With limit and offset, everything is clear, we pass them during the request or set the default values ​​in which case. But what about the total number of entries? It's just that items.size doesn't fit since it will only return 20 rows, the limit we passed in the request. I don’t quite understand where to get this value from, is it really necessary to make two requests without a limit and consider what count (*) will return at the database level (which can be costly and dangerous) and then select the data? Or organize all the limits at the Java level and not at the SQL level? Which, to be honest, is also not a very good idea.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-10-28
@Sad_Bro

count() is usually fast and doesn't select anything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question