在 RESTful 服务中部分更新复杂类型
我正在将 JSON 与 RESTful 服务结合使用。实现是这样的。
http://hostname/a 上的 GET 返回
{
"a": {
"b": {
"c1": "data1",
"c2": "data2"
}
}
}
{
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您偶然发现了我在过去几年处理 REST 时遇到的最具争议性的问题之一。
这是简单的答案:
普遍的共识是 HTTP PUT 方法具有替换语义,因此 c2 被覆盖并且不再存在。
最近引入了 PATCH 方法来帮助人们处理部分更新。
现在,该场景有两个复杂之处:
实际上,HTTP 规范并没有明确规定 PUT 具有替换语义,它 说:
,它还说
就我个人而言,我避免使用 PUT,因为我发现“替换”语义太有限制。然而,最近我开始被 Mike Kelly 和 Mike 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:
Actually, the HTTP spec does not specifically say PUT has replace semantics, it says:
however, it also says
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.
首先,如果您更改“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.
如果您 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.