RESTful方式返回位置信息

发布于 2024-12-07 17:00:59 字数 575 浏览 1 评论 0原文

我编写了一个基于 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 技术交流群。

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

发布评论

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

评论(1

栩栩如生 2024-12-14 17:00:59

这是设计超媒体 API 时非常常见的做法。这也是强烈推荐的。没有官方的“正确”方法来做到这一点,但是一种对于如何做到这一点有非常明确的规则的媒体类型是 hal。请参阅 http://stateless.co/hal_specation.html

在 hal 中,您的文档将如下所示:

<resource href="/users">
    <resource rel="item" href="/users/1234">
        ...
    </resource>
    <resource rel="item" href="/users/5678">
        ...
    </resource>
</resource>

我选择术语“项目”,因为它已经被伪标准化,但您可以放置​​自己的链接关系,在识别自己的链接关系时,您应该遵循一些规则。 (详情请参阅 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:

<resource href="/users">
    <resource rel="item" href="/users/1234">
        ...
    </resource>
    <resource rel="item" href="/users/5678">
        ...
    </resource>
</resource>

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)

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