我正在为客户管理系统编写一个RESTful服务,并试图找到部分更新记录的最佳实践。例如,我希望调用者能够使用GET请求读取完整的记录。但是为了更新它,只允许对记录进行某些操作,比如将状态从ENABLED更改为DISABLED。(我有比这更复杂的场景)

出于安全原因,我不希望调用者只提交更新字段的整个记录(这也感觉有点过分)。

是否有构造uri的推荐方法?在阅读REST书籍时,RPC样式的调用似乎是不受欢迎的。

如果下面的调用返回id为123的客户的完整客户记录

GET /customer/123
<customer>
    {lots of attributes}
    <status>ENABLED</status>
    {even more attributes}
</customer>

我该如何更新状态?

POST /customer/123/status
<status>DISABLED</status>

POST /customer/123/changeStatus
DISABLED

...

更新:扩充问题。如何将“业务逻辑调用”合并到REST api中?有没有大家都同意的做法?并非所有的方法本质上都是CRUD。有些更复杂,如“sendEmailToCustomer(123)”,“mergeCustomers(123, 456)”,“countCustomers()”

POST /customer/123?cmd=sendEmail

POST /cmd/sendEmail?customerId=123

GET /customer/count 

当前回答

你基本上有两个选择:

Use PATCH (but note that you have to define your own media type that specifies what will happen exactly) Use POST to a sub resource and return 303 See Other with the Location header pointing to the main resource. The intention of the 303 is to tell the client: "I have performed your POST and the effect was that some other resource was updated. See Location header for which resource that was." POST/303 is intended for iterative additions to a resources to build up the state of some main resource and it is a perfect fit for partial updates.

其他回答

使用PUT更新不完整/部分的资源。

您可以接受jObject作为参数,并解析其值以更新资源。

下面是你可以参考的Java函数:

public IHttpActionResult Put(int id, JObject partialObject) {
    Dictionary < string, string > dictionaryObject = new Dictionary < string, string > ();

    foreach(JProperty property in json.Properties()) {
        dictionaryObject.Add(property.Name.ToString(), property.Value.ToString());
    }

    int id = Convert.ToInt32(dictionaryObject["id"]);
    DateTime startTime = Convert.ToDateTime(orderInsert["AppointmentDateTime"]);
    Boolean isGroup = Convert.ToBoolean(dictionaryObject["IsGroup"]);

    //Call function to update resource
    update(id, startTime, isGroup);

    return Ok(appointmentModelList);
}

您应该使用POST进行部分更新。

要更新客户123的字段,需要POST到/customer/123。

如果你只想更新状态,你也可以PUT到/customer/123/status。

通常,GET请求不应该有任何副作用,而PUT用于写入/替换整个资源。

这直接来自HTTP,如这里所示:http://en.wikipedia.org/wiki/HTTP_PUT#Request_methods

我也遇到了类似的问题。当您只想更新单个字段时,子资源上的PUT似乎可以工作。然而,有时你想要更新一堆东西:考虑一个web表单表示资源,选项更改一些条目。用户提交的表单不应该导致多个put。

以下是我能想到的两种解决方案:

对整个资源执行PUT。在服务器端,定义具有整个资源的PUT忽略所有未更改的值的语义。 使用部分资源执行PUT。在服务器端,将此语义定义为合并。

2就是1的带宽优化。有时1是唯一的选项,如果资源定义了一些字段是必需的字段(想想proto缓冲区)。

这两种方法的问题是如何清除字段。您必须定义一个特殊的空值(特别是对于proto缓冲区,因为没有为proto缓冲区定义空值),这将导致字段清除。

评论?

没关系。就REST而言,你不能做GET,因为它不是可缓存的,但不管你使用POST, PATCH, PUT或其他什么都没关系,URL看起来是什么也没关系。如果您正在使用REST,那么重要的是当您从服务器获得资源的表示时,该表示能够提供客户端状态转换选项。

如果您的GET响应有状态转换,客户机只需要知道如何读取它们,服务器可以根据需要更改它们。这里的更新是使用POST完成的,但是如果它被更改为PATCH,或者如果URL更改了,客户端仍然知道如何进行更新:

{
  "customer" :
  {
  },
  "operations":
  [
    "update" : 
    {
      "method": "POST",
      "href": "https://server/customer/123/"
    }]
}

你可以列出必要的/可选的参数,让客户端反馈给你。这取决于应用程序。

就业务操作而言,这可能是与客户资源链接的不同资源。如果你想发送一封电子邮件给客户,也许该服务是它自己的资源,你可以POST,所以你可以在客户资源中包括以下操作:

"email":
{
  "method": "POST",
  "href": "http://server/emailservice/send?customer=1234"
}

以下是演示者REST架构的一些优秀视频和示例。Stormpath只使用GET/POST/DELETE,这很好,因为REST与你使用的操作或url的外观没有任何关系(除了GET应该是可缓存的):

https://www.youtube.com/watch?v=pspy1H6A3FM, https://www.youtube.com/watch?v=5WXYw4J4QOU, http://docs.stormpath.com/rest/quickstart/

你基本上有两个选择:

Use PATCH (but note that you have to define your own media type that specifies what will happen exactly) Use POST to a sub resource and return 303 See Other with the Location header pointing to the main resource. The intention of the 303 is to tell the client: "I have performed your POST and the effect was that some other resource was updated. See Location header for which resource that was." POST/303 is intended for iterative additions to a resources to build up the state of some main resource and it is a perfect fit for partial updates.