简体   繁体   English

是否应该在RESTful API中对单个项目进行分页?

[英]Should single items be paginated in a RESTful API?

Discussion at work has become quite divisive. 工作中的讨论变得很分裂。

I request a collection from /books 我要求从/books收藏

{
    "offset": 0,
    "limit": 10,
    "total": 3,
    "results": [
        {
            "id" : 1,
            "title" : "Book 1"
        },
        {
            "id" : 2,
            "title" : "Book 2"

        },
        {
            "id" : 3,
            "title" : "Book 3"

        }
    ]
}

This is paginated. 这是分页的。

Now if I request to books/<id> for a specific resource, should this be wrapped in a pagination results block too? 现在,如果我请求books/<id>获取特定资源,是否也应该将其包装在分页结果块中?

Some think that everything should be paginated but some think that a request to a unique item should respond with that and just that. 有些人认为应该对所有内容进行分页,但有些人认为对唯一商品的请求应以此为准。 So... 所以...

{
    "id" : 1,
    "title" : "Book 1"
}

This makes the most sense to me, as you're not requesting a collection you're requesting that specific item, why would a single resource need to be paginated? 这对我来说最有意义,因为您不要求收藏,而是要求特定的物品,为什么要对单个资源进行分页?

I can understand this for the sake of consistency for the clients but I just want other opinions as I understand there's no right implementation of this obviously. 为了使客户保持一致,我可以理解这一点,但是我只想征询其他意见,因为我知道显然没有正确的实现方法。

Thanks in advance, 提前致谢,

Looking forward to everyone's points :) 期待大家的观点:)

We've all been there :), thanks to standards, things are much more consistent and cleaner of course. 我们都去过那里:),由于有了标准,所以事情变得更加一致和清晰。 i personally apply the jsonapi.org v1.0 standard, which helped me a lot to make my restful services "standardized". 我个人应用了jsonapi.org v1.0标准,这对我使静态服务“标准化”有很大帮助。

to answer your question, i think, if you are requesting one entity, well.. wrapping it with pagination is an extra work and you will not use it. 回答您的问题,我想,如果您要一个实体,那么..分页包装是一项额外的工作,您将不会使用它。 the way i see it, is that you asked the restfull server to return only one entity, so why returning the pages, you dont need them, you requested one entity, you are aware of it! 我的看法是,您要求restfull服务器仅返回一个实体,因此为什么返回页面,您不需要它们,您请求了一个实体,您就知道了! you should handle it. 你应该处理。 programming logic, only return/use what you need, unless you have an exception. 编程逻辑,除非有例外,否则仅返回/使用您需要的内容。 ( and that another issue, re-factor your app architecture ). (这是另一个问题,请重构您的应用架构)。

hope this helps :) 希望这可以帮助 :)

I second the answer. 我第二个答案。 Don't return the pagination for the single entity. 不要返回单个实体的分页。 The structure can imply as much meaning as the result itself. 结构可以暗示与结果本身一样多的含义。 It may be important to note that the list itself returned only a single result - hence pagination vs. requesting a single result and getting exactly that one back. 可能需要注意的是,列表本身仅返回单个结果-因此分页与请求单个结果并准确返回该结果。

Too many times I have seen in the past where bugs crop up where it's difficult to discern because of these 'optimizations'. 在过去,我见过太多次了,因为这些“优化”,虫子越来越难以辨认。 Stick to your strong API definitions and it will pay dividends in not wasting hours debugging. 坚持使用强大的API定义,它将在不浪费时间进行调试的情况下带来收益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM