[英]REST API design: linking resources
Supposed I have two top-level resources Foo
and Bar
. 假设我有两个顶级资源
Foo
和Bar
。 Now the Foo
need to be linked to some of the Bar
. 现在
Foo
需要链接到某些Bar
。 In a Java class this might look something like this: 在Java类中,可能看起来像这样:
public class Foo {
Set<Bar> bars;
}
public class Bar { … }
I'd like to shape the XML representation of Foo
to something like this: 我想将
Foo
的XML表示形式调整为以下形式:
GET /foos/1
<foo>
…
<atom:link rel="self" href="/foos/1" />
<atom:link rel="bars" href="/foos/1/bars" />
</foo>
So I pretty much expose all Bar
assigned to a Foo
as nested resource. 因此,我几乎将分配给
Foo
所有Bar
作为嵌套资源公开。 That means Bar
resource have an individual lifecycle (aggregation instead of composition). 这意味着
Bar
资源具有单独的生命周期(聚合而不是组合)。 The nested resource might then expose all linked Bar
something like this: 然后,嵌套的资源可能会公开所有链接的
Bar
,如下所示:
GET /foos/1/bars
<bars>
<atom:link rel="bar" href="/foos/1/bars/1" />
<atom:link rel="bar" href="/foos/1/bars/2" />
</bars>
Alteratively I could inline the collection in the <foo>
element upfront. 或者,我可以预先在
<foo>
元素中内联该集合。 However I am still stuck with some questions: While this allows me to nicely remove a Bar
from a Foo
by triggering a DELETE
request to eg /foos/1/bars/1
but how would one assign a Bar
to a Foo
then? 但是我仍然遇到一些问题:尽管这允许我通过触发对
/foos/1/bars/1
的DELETE
请求从Foo
很好地删除Bar
,但是那又如何将Bar
分配给Foo
呢? Assuming the client would access /bars
getting: 假设客户端将访问
/bars
得到:
GET /bars
<bars>
<bar>
…
<atom:link rel="self" href="/bars/4711" />
</bar>
</bars>
and deciding it wants to assign /bars/1
to /foo/1/bars
. 并决定将
/bars/1
分配给/foo/1/bars
。 I was thinking about a POST
request to /foo/1/bars
but was unsure about what to actually submit. 我在考虑对
/foo/1/bars
的POST
请求,但不确定实际提交的内容。 A link
element pointing to the Bar
resource such as the following? 指向
Bar
资源的link
元素,例如以下?
POST /foos/1/bars
<atom:link href="/bars/4711" />
This seems quite okay as the clients would still not need to create URLs and we still satisfy REST constraints. 这似乎还可以,因为客户端仍然不需要创建URL,并且我们仍然满足REST约束。 However it feels a bit weird to
POST
links to the server. 但是,
POST
链接到服务器有点奇怪。 Is there a better solution to this scenario? 对于这种情况有更好的解决方案吗?
I think about this in terms of the resources understood by the server rather than the XML representations that flow in response to (say) a GET request. 我考虑服务器理解的资源,而不是响应(例如)GET请求而流动的XML表示形式来考虑。 I have RESTful services that return either JSON or XML, or potentially other representations.
我有RESTful服务,可以返回JSON或XML,或可能返回其他表示形式。
So I agree with your POSTing or PUTing to 因此,我同意您的POST或PUT
/foos/{fooId}/bars
to specify either the complete list of bars or the addition of some bars. 指定条的完整列表或添加一些条。
The format of the posted payload can be whatever is the natural serialised form for the media-type your're using. 发布的有效负载的格式可以是您所使用的媒体类型的自然序列化格式。 In my case it's often a JSON string and so in my service implementation would deserialise and see an array of resoruce reference URL strings.
就我而言,它通常是一个JSON字符串,因此在我的服务实现中会反序列化并看到一组重新排列的参考URL字符串。
If your 如果你的
<atom:link href="/bars/4711" />
also deserialise nicely then I don't see a problem if the serialised form is a little, um, ornamental. 还很好地反序列化,那么如果序列化的格式有点装饰性,我看不出任何问题。
Summary: do what's natural for your (de)serialiser. 简介:对您的(反)序列化器来说很自然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.