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

查看:87
本文介绍了以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,但是PUTPOST对于整个列表都没有真正意义,除非我提交包含新数字的整个集合作为消息数据.

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?

推荐答案

在拉菲人对我的最初回应发表评论后,我重新设计了答案,使其更加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.

    使用标题方法,您将拥有类似的内容:

    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" },
                (...)
            ]
        }
    }
    

    希望它对您有帮助, 蒂埃里

    Hope it helps you, Thierry

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

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