在 RESTful 服务中部分更新复杂类型

发布于 2024-10-11 09:26:42 字数 699 浏览 7 评论 0原文

我正在将 JSON 与 RESTful 服务结合使用。实现是这样的。

http://hostname/a 上的 GET 返回

{
    "a": {
        "b": {
            "c1": "data1",
            "c2": "data2" 
        } 
    }
}

http://hostname/a/b 返回

{
    "b": {
        "c1": "data1",
        "c2": "data2" 
    } 
}

我想知道 http://hostname/a

{
    "a": {
        "b": {
            "c1": "newdata" 
        } 
    }
}

它应该只用值“newdata”更新c1还是应该替换整个资源b,从而只包含c1(即c2被覆盖并且不再存在)

I am using JSON with RESTful service. Implementation is like this.

GET on http://hostname/a returns

{
    "a": {
        "b": {
            "c1": "data1",
            "c2": "data2" 
        } 
    }
}

GET on http://hostname/a/b returns

{
    "b": {
        "c1": "data1",
        "c2": "data2" 
    } 
}

I wanted to know the correct behavior of POST(and PUT) on http://hostname/a

{
    "a": {
        "b": {
            "c1": "newdata" 
        } 
    }
}

Should it just update the c1 with value "newdata" or it should replace the whole resource b, thereby just containing c1 ( i.e. c2 is overwritten and doesn't exist anymore)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-10-18 09:26:42

您偶然发现了我在过去几年处理 REST 时遇到的最具争议性的问题之一。

这是简单的答案:
普遍的共识是 HTTP PUT 方法具有替换语义,因此 c2 被覆盖并且不再存在。
最近引入了 PATCH 方法来帮助人们处理部分更新。

现在,该场景有两个复杂之处:

  1. 为什么 PUT 必须具有替换语义?进行部分更新有哪些负面影响?我还没有听到真正令人信服的论据。

实际上,HTTP 规范并没有明确规定 PUT 具有替换语义,它

PUT 方法要求
封闭的表示存储在
有效请求URI

,它还说

HTTP/1.1 没有定义 PUT 的方式
方法影响原点状态
服务器。

  1. 如果您假设 PUT 具有替换语义,则服务器可能会在未替换的表示中包含某些内容,这是可以接受的。例如,如果表示包含链接,则对不包含这些链接的表示执行 PUT 不会“删除”这些链接。对于时间戳字段或上次修改的数据也是如此。永恒的问题是我们如何定义哪些内容在客户端省略时被删除,哪些内容因为服务器这么说而保留!

就我个人而言,我避免使用 PUT,因为我发现“替换”语义太有限制。然而,最近我开始被 Mike KellyMike Amundsen 认为 PUT 可能比我们目前认为的更灵活。

You have hit upon one of the most debated issues that I have seen in my time dealing with REST over the last few years.

Here's the simplistic answer:
The general consensus is that the HTTP PUT method has replace semantics and therefore c2 is overwritten and does not exist anymore.
The PATCH method was recently introduced to help people deal with partial updates.

Now, here are two complications to the scenario:

  1. Why must PUT have replace semantics? What are the negative effects of doing partial updates? I've yet to hear a really convincing argument.

Actually, the HTTP spec does not specifically say PUT has replace semantics, it says:

The PUT method requests that the
enclosed representation be stored at
the effective request URI

however, it also says

HTTP/1.1 does not define how a PUT
method affects the state of an origin
server.

  1. It is accepted that if you assume PUT has replace semantics that a server may include some content in a representation that is not replaced. e.g. If a representation contains links, doing a PUT of a representation that does not contain those links does not "delete" those links. Same for a timestamp field, or last modified data. The eternal question is how do we define which content is removed when omitted by the client and which stays because the server says so!

Personally, I avoided PUT because I found the "replace" semantics too constraining. However, recently I am starting to be convinced by Mike Kelly and Mike Amundsen that maybe PUT should be considered more flexible than we currently give it credit for.

能怎样 2024-10-18 09:26:42

首先,如果您更改“b”资源,为什么不将 POST(或 PUT)发送到 http://hostname/ a/b 而不是 http://hostname/a ?如果某个东西值得被指向/链接,那么根据 RIA 哲学,它应该作为资源来实现,并且有它自己的 URI。

First of all, if you change a 'b' resource, why not you send POST(or PUT) to http://hostname/a/b instead of just http://hostname/a ? If something worth to be pointed/linked, it should be implemented as resource, according to RIA philosophy, and has got it's own URI.

故人的歌 2024-10-18 09:26:42

如果您 PUT 到 http://hostname/a http://主机名/a/b?它们允许不同步多长时间?

如果答案是“它们没有被缓存”......?

“分层系统的主要缺点是它们增加了数据处理的开销和延迟,从而降低了用户感知的性能。对于支持缓存约束的基于网络的系统,这可以通过中介共享缓存的好处来抵消。 ” ——一篇著名的论文。

If you PUT to http://hostname/a what happens to cached copies of http://hostname/a/b? How long are they allowed to be out of sync?

If the answer is "they're not cached"...?

"The primary disadvantage of layered systems is that they add overhead and latency to the processing of data, reducing user-perceived performance. For a network-based system that supports cache constraints, this can be offset by the benefits of shared caching at intermediaries." -- A famous dissertation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文