Mep*_*pla 5 rest pagination api-design mobile-application restful-architecture
我正在为我正在开发的移动应用程序设计RESTful API.我的问题是包含许多项目的大型集合.我知道一个好的做法是在集合中对大量结果进行分页.
我已经阅读了Facebook Graph API文档(https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2),Twitter游标文档(https://dev.twitter.com/概述/ api/cursoring),GitHub API doc(https://developer.github.com/v3/)和这篇文章(API分页最佳实践).
考虑/resources
我的API中的示例集合,其中包含100个名为resource1
to resource100
和sorted descending的项目.这是您在GET请求时得到的响应(GET http://api.path.com/resources?limit=5
):
{
"_links": {
"self": { "href": "/resources?limit=5&page=1" },
"last": { "href": "/resources?limit=5&page=7" },
"next": { "href": "/resources?limit=5&page=2" }
},
"_embedded": {
"records": [
{ resource 100 },
{ resource 99 },
{ resource 98 },
{ resource 97 },
{ resource 96 }
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是这样的场景:
1- I获取/resources
上述内容.
2-之后,会向资源集合添加一些内容(例如,另一台设备会为此帐户添加新资源).所以现在我有101个资源.
3- I GET,/resources?limit=5&page=2
因为最初的回复建议将包含我的结果的下一页.答案如下:
{
"_links": {
"self": { "href": "/history?page=2&limit=5" },
"last": { "href": "/history?page=7&limit=5" },
"next": { "href": "/history?page=3&limit=5" }
},
"_embedded": {
"records": [
{ resource 96 },
{ resource 95 },
{ resource 94 },
{ resource 93 },
{ resource 92 }
]
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见resource 96
,在两个页面中都会重复(或者如果在步骤2中删除资源,则可能会发生类似问题,在这种情况下,将丢失一个资源).
由于我想在移动应用程序和一个列表中使用它,我必须将每个API调用的资源附加到它之前的那个,这样我就可以有一个完整的列表.但这令人不安.如果您有任何建议,请告诉我.先感谢您.
PS:我已经考虑过像查询字符串这样的时间戳而不是基于游标的分页,但这会让我在其他地方遇到问题.(如果您需要更多相关信息,请与我们联系.)