REST Web应用程序中的分页 [英] Pagination in a REST web application

查看:56
本文介绍了REST Web应用程序中的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的更通用的表述. (省去了Rails的特定部分)

我不确定如何在RESTful Web应用程序中的资源上实现分页. 假设我有一个名为products的资源,您认为以下哪种方法是最好的方法,以及原因:

1.仅使用查询字符串

例如http://application/products?page=2&sort_by=date&sort_how=asc
这里的问题是我无法使用全页缓存,而且URL也不是很干净且易于记忆.

2.使用页面作为资源和查询字符串进行排序

例如http://application/products/page/2?sort_by=date&sort_how=asc
在这种情况下,看到的问题是http://application/products/pages/1不是唯一资源,因为使用sort_by=price可以产生完全不同的结果 ,但我仍然无法使用页面缓存

3.使用页面作为资源和URL片段进行排序

例如http://application/products/by-date/page/2
我个人认为使用此方法没有问题,但是有人警告我这不是一个好方法(他没有给出原因,所以如果你知道为什么 推荐,请让我知道)

任何的建议,观点和批评都非常受欢迎.谢谢.

解决方案

我认为版本3的问题更多是观点"问题-您将页面视为页面上的资源还是产品. /p>

如果您将页面视为资源,那是一个很好的解决方案,因为对页面2的查询将始终产生页面2.

但是,如果您将页面上的产品视为资源,则会遇到问题,即页面2上的产品可能会发生变化(删除了旧产品或其他原因),在这种情况下,URI并不总是 返回相同的资源.

例如客户存储了指向产品列表页面X的链接,下次打开链接时,所涉及的产品可能不再位于页面X上.

This is a more generic reformulation of this question (with the elimination of the Rails specific parts)

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:

1. Using only query strings

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.

2. Using pages as resources and query strings for sorting

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.

3. Using pages as resources and an URL segment for sorting

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.

解决方案

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.

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.

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).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆