简体   繁体   English

如何“正确地”将现有实体“正确”添加到集合中

[英]How to “correctly” add an existing entity to a collection RESTfully

I have two already extant objects, 我有两个已经存在的对象,

/foo/1 GET
/bar/1 GET

Each foo has bar s and each bar has foo s. 每个foo都有bar ,每个bar都有foo How do I RESTfully add my bar to my foo 's bar collection? 如何将我的bar RESTful添加到foobar集合中?

This seems like one possible interpretation: 这似乎是一种可能的解释:

/foo/1 PATCH { bars: [1] }

but it breaks the idea that bar s attached to that foo should be displayed via /foo/1/bar/ . 但这打破了应该通过/foo/1/bar/显示附加到foo bar的想法。 This also seems valid: 这似乎也是有效的:

/foo/1/bar/ POST { id: 1, ... }

and fits closest to the wikipedia description, but since we're not creating a new bar this is a bit confusing. 并最接近Wikipedia描述,但由于我们没有创建新的bar因此有点令人困惑。 This also creates a kind of bizarre ambiguity, since it may also be possible to create a new bar assigned to this foo via this method. 这也造成了一种奇怪的歧义,因为也有可能通过此方法创建分配给该foo的新bar Lastly, there's: 最后,有:

/foo/1/bar/1 POST

which I just plain like due to its simplicity, but doesn't agree with the wikipedia description and gives no obvious way to remove the bar from the list (since DELETE would delete the bar ). 由于其简单性,我只是简单地喜欢它,但是与Wikipedia描述不同,并且没有提供从列表中删除该bar明显方法(因为DELETE会删除该bar )。

I realize REST is an architectural style rather than an explicit standard, but there must be some least-problematic way to do this. 我意识到REST是一种体系结构样式,而不是明确的标准,但是必须有一些问题最少的方法来实现。

I usually see this solved by using a third resource to own the mapping between foo and bar, such as: 我通常看到这是通过使用第三个资源来拥有foo和bar之间的映射来解决的,例如:

POST /foo
<- "id": 22

POST /bar
<- "id": 17

POST /foo-bars
-> { "fooId": 22, "barId": 17 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM