REST 和链接:中间地带? [英] REST and links: middle ground?

查看:45
本文介绍了REST 和链接:中间地带?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道 REST 中的链接能走多远.考虑有作者的书,但书和作者之间显然是多对多的关系(一本书可以由多个作者撰写,而作者可以写多本书).

I've been wondering about how far to go with links in REST. Consider books which have authors, but there is obviously a many-to-many relationship between books an authors (a book can be written by multiple authors, and authors can write multiple books).

假设我们有一个rest调用http://server/book/21,它将返回一个包含作者信息的书籍XML.现在既然书是资源,作者是资源,XML 不应该直接包含所有作者信息.它应该包含指向作者信息的链接.但是下面两个例子中哪个更被广泛接受?

So let's say we have a rest call http://server/book/21, which will return a book XML, containing information about an author. Now since the book is a resource, and the author is a resource, the XML should not straight up include all the author information. It should contain a link to the author information. But which of the below two examples is more widely accepted?

(原谅我糟糕的 XML 格式,我没有手写 XML 的经验)

(Excuse my crappy formatted XML, I am not that experienced with hand writing XML)

<book>
  <title>Some Book</title>
  <authors>
    <author link="http://server/author/82">Some Guy</author>
    <author link="http://server/author/51">Some Other Guy</author>
  </authors>
</book>

然后,作者链接将返回更多信息:

Then, an author link would return more information:

<author>
  <name>Some Guy</name>
  <dateOfBirth>some time</dateOfBirth>
</author>

或者:

<book>
  <title>Some Book</title>
  <authors>http://server/book/21/authors</authors>
</book>

http://server/book/21/authors 返回的地方:

<authors>
  <author link="http://server/author/82">Some Guy</author>
  <author link="http://server/author/51">Some Other Guy</author>
</authors>

然后每个都再次返回之前的 示例.

And then each of those returns the former <author> example again.

我之所以这么问,主要是因为在我的工作中,他们采用了第二种方法,在我看来,客户必须采取更多步骤才能到达他们想去的地方.此外,对于您总是需要"(作者姓名)的基本信息,您必须采取额外的步骤.另一方面,这样 book 资源只返回关于这本书的信息(没有别的),要得到其他任何东西,你必须访问其他资源.

The reason I'm asking is basically because at my job they went with the second approach, and it seems to me that clients have to take many more steps to reach where they want to go. Also, for basic information which "you're always going to need" (author's name), you do have to take one additional step. On the other hand, that way the book resource only returns information about the book (nothing else), and to get anything else, you have to access other resources.

推荐答案

这个问题听起来更像是您的偏好是什么"类型的问题.所以这是我的 2 美分:

This question sounds more like a "what is your preference" type of question. So here is my 2 cents:

在我看来,在原始 xml 中包含作者姓名将是最好的主意.这将允许客户端应用程序显示可热链接的作者姓名列表,而无需第二个休息请求.当图书资源出现时,作者姓名很可能始终显示.如果我是你,我会更关注实用性,而不是担心其他资源的理论"正确性.如果这有任何意义.

It seems to me like including the author names in the original xml will be the best idea. This will allow the client application to display a list of hot-linkable author names, without requiring a second rest request. Author names are most likely always displayed when the book resource is. If I were you I would focus more on the practicality, than worrying about the "theoretical" correctness of the rest resource. If that makes any sense.

您不必在原始 xml 资源中包含作者的所有信息.而是显示图书资源以及在需要时/如果需要时查找有关作者的更多信息的实用方法.

You don't have to include all of the author's information in the original xml resource. Rather what is practical for displaying the book resource, and finding more information about the author when/if needed.

这篇关于REST 和链接:中间地带?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆