REST Web 应用程序中的分页 [英] Pagination in a REST web application
问题描述
这是对这个问题的更通用的重新表述(消除了 Rails 特定部分)
This is a more generic reformulation of this question (with the elimination of the Rails specific parts)
我不确定如何在 RESTful Web 应用程序中的资源上实现分页.假设我有一个名为 products
的资源,您认为以下哪种方法是最好的,为什么:
I am not sure how to implement pagination on a resource in a RESTful web application.
Assuming that I have a resource called products
, which of the following do you think is the best approach, and why:
例如.http://application/products?page=2&sort_by=date&sort_how=asc
这里的问题是我无法使用整页缓存,而且 URL 不是很干净且易于记忆.
eg. http://application/products?page=2&sort_by=date&sort_how=asc
The problem here is that I can't use full page caching and also the URL is not very clean and easy to remember.
例如.http://application/products/page/2?sort_by=date&sort_how=asc
在这种情况下,看到的问题是 http://application/products/pages/1
不是唯一资源,因为使用了 sort_by=price
可以产生完全不同的结果而且我仍然无法使用页面缓存.
eg. http://application/products/page/2?sort_by=date&sort_how=asc
In this case, the problem that is see is that http://application/products/pages/1
is not a unique resource since using sort_by=price
can yield a totally different result and I still can't use page caching.
例如.http://application/products/by-date/page/2
我个人认为使用这种方法没有问题,但有人警告我这不是一个好方法(他没有给出理由,所以如果你知道为什么推荐,请告诉我)
eg. http://application/products/by-date/page/2
I personally see no problem in using this method, but someone warned me that this is not a good way to go (he didn't give a reason, so if you know why it's not recommended, please let me know)
任何建议、意见、批评都非常受欢迎.谢谢.
Any suggestions, opinions, critiques are more than welcome. Thanks.
推荐答案
我认为版本 3 的问题更像是一个观点"问题——你将页面视为资源还是页面上的产品.
I think the problem with version 3 is more a "point of view" problem - do you see the page as the resource or the products on the page.
如果您将页面视为资源,这是一个完美的解决方案,因为对第 2 页的查询将始终产生第 2 页.
If you see the page as the resource it is a perfectly fine solution, since the query for page 2 will always yield page 2.
但是,如果您将页面上的产品视为资源,您就会遇到第 2 页上的产品可能会更改的问题(删除旧产品或其他),在这种情况下,URI 并不总是返回相同的资源.
But if you see the products on the page as the resource you have the problem that the products on page 2 might change (old products deleted, or whatever), in this case the URI is not always returning the same resource(s).
例如客户存储了指向产品列表页面 X 的链接,下次打开该链接时,相关产品可能不再位于页面 X 上.
E.g. A customer stores a link to the product list page X, next time the link is opened the product in question might no longer be on page X.
这篇关于REST Web 应用程序中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!