以 REST 方式更新整个资源集合 [英] Update an entire resource collection in a REST way

查看:21
本文介绍了以 REST 方式更新整个资源集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源列表的 REST URI,例如:

I have a REST URI for a list of resources, something like:

http://foo.com/group/users

这些用户中的每一个都有一个序列号,我想公开一种方法来为集合中的所有用户重新编号这些值,并使访问列表的每个人都可以使用此更改.由于这是对整个集合的操作,我不确定如何完成此操作.

Each of these users has a sequence number and I want to expose a way to renumber those values for all the users in the collection and make this change available to everyone who accesses the list. Since this is an action on the collection as a whole, I'm not sure how to accomplish this.

我可以设想一个像 http://foo.com/group/users?sequence=normalize 这样的 URL,但既不是 PUT 也不是 POST 对整个列表确实有意义,除非我使用新数字作为消息数据提交整个集合.

I can envision a URL like http://foo.com/group/users?sequence=normalize but neither a PUT nor a POST really makes sense for the whole list, unless I submit the whole collection with the new numbers as the message data.

如何以 RESTful 方式更新整个集合,而不必重新发送集合中的所有更新资源?

How can I make an update to an entire collection like this in a RESTful way without having to resend all the updated resources in the collection?

推荐答案

在 raffian 对我最初的回复发表评论后,我重新编写了我的答案,使其更加 RESTful...

After the raffian's comment on my initial response, I reworked my answer to be more RESTful...

  • 使用方法PATCH

此方法通常旨在部分更新资源的状态.在列表资源的情况下,我们可以发送一个列表,其中只包含要更新的元素和列表中元素的标识符.以下请求将是:

This method is typically designed to update partially the state of a resource. In the case of a list resource, we could send a list with only the elements to update and the identifiers of elements in the list. The following request would be:

PATCH /group/users
[
    { "id": "userId1", "sequence": "newSequenceNumber1" },
    { "id": "userId2", "sequence": "newSequenceNumber2" },
    (...)
]

  • 在列表资源上使用POST方法
  • 这个方法通常用于在资源管理的列表中添加一个元素.因此,如果您想将其用于此操作,我们需要在请求中传递有关要执行的操作的提示.我们可以选择将其添加到专用标头中或负载中.

    This method is commonly used to add an element in the list managed by the resource. So if you want to leverage it for this action, we need to pass within the request an hint regarding the action to execute. We have the choice to add this either in a dedicated header or within the payload.

    使用 header 方法,你会得到类似的东西:

    With the header approach, you will have something like that:

    POST /group/users
    X-Action: renumbering
    [
        { "id": "userId1", "sequence": "newSequenceNumber1" },
        { "id": "userId2", "sequence": "newSequenceNumber2" },
        (...)
    ]
    

    使用有效载荷方法,您将获得类似的结果:

    With the payload approach, you will have something like that:

    POST /group/users
    {
        "action": "renumbering",
        "list": {
            [
                { "id": "userId1", "sequence": "newSequenceNumber1" },
                { "id": "userId2", "sequence": "newSequenceNumber2" },
                (...)
            ]
        }
    }
    

    希望对你有帮助蒂埃里

    这篇关于以 REST 方式更新整个资源集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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