RESTful方式返回位置信息
我编写了一个基于 RESTEasy 的 REST 服务器。
在某些资源中,返回对象集合而不是单个对象:响应 URL“/users/{user_id}
”的方法返回单个“user
” ”对象,但响应 URL“/users
”的方法会返回所有用户。在前一种情况下,我可以包含一个 HTTP 标头“Content-Location: URL
”(尽管这是多余的,因为调用者具有正确的 URL)。在后一种情况下,返回集合中的每个项目都有其自己的位置。我正在考虑向每个项目添加一个 XML 属性来指定其位置:
<users>
<user location="/users/1234">
...
</user>
<user location="/users/5678">
...
</user>
</users>
是否有一些返回此信息的常规方法?
I have written a REST server based on RESTEasy.
In some of the resources, collections of objects are returned instead of single objects: the method that responds to the URL "/users/{user_id}
" returns a single "user
" object, but the method that responds to the URL "/users
" returns all users. In the former case, I can include an HTTP header "Content-Location: URL
" (although this is redundant since the caller has the correct URL). In the latter case, each item in the returned collection has its own location. I was thinking of added an XML attribute to each item specifying its location:
<users>
<user location="/users/1234">
...
</user>
<user location="/users/5678">
...
</user>
</users>
Is there some conventional way of returning this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计超媒体 API 时非常常见的做法。这也是强烈推荐的。没有官方的“正确”方法来做到这一点,但是一种对于如何做到这一点有非常明确的规则的媒体类型是 hal。请参阅 http://stateless.co/hal_specation.html
在 hal 中,您的文档将如下所示:
我选择术语“项目”,因为它已经被伪标准化,但您可以放置自己的链接关系,在识别自己的链接关系时,您应该遵循一些规则。 (详情请参阅 RFC5988)
This is a very common practice when designing hypermedia APIs. It is also highly recommended. There is no official "right" way of doing it, however one media type that has very explicit rules as to how you can do it is hal. See http://stateless.co/hal_specification.html
In hal your document would look like:
I picked the term "item" because it has been psuedo standardized, but you could put your own link relation, there are just a few rules you should follow when identifying your own link relations. (See RFC5988 for details)