如果对象之间没有 id,RESTful url 是否有效?

发布于 2024-10-08 18:56:18 字数 673 浏览 1 评论 0原文

我有两个 RESTful API 设计问题。假设我有一个水果摊网络应用程序。我想返回有关我携带的所有水果以及这些水果的数量的信息,我使用以下方法执行此操作:

http://myfruitstand.com/fruits

问题 1: 如果我有 10 个橙子,那么我想我可以通过以下方式获取有关特定橙子的信息:

http://myfruitstand.com/fruits/oranges/3

但是上面的 url 是否是 RESTful——我不需要在“fruits”和“oranges”之间添加一个 id 来符合 REST 标准吗?或者这个带有 'fruits' 的 url 是否立即跟随子类 'oranges' 好吗?

问题 2: 同样,如果我想有一个关于我的橙子(不是特定的橙子)的讨论论坛,我可以把它放在这里吗:

http://myfruitstand.com/fruits/oranges/comments

同样,上面的 url 是 RESTful,因为“橙子”和“评论”之间没有 id(这里是 id,当然,这意味着要讨论某个特定的橙子,而我不希望这样)?在这里,不存在紧随“评论”的“橙色”的子类理性。

预先感谢,查克

I have two RESTful API design questions. Let's say I have a fruitstand web app. I want to return information about all the fruits I carry and the counts those fruits, and I do it with:

http://myfruitstand.com/fruits

Question 1:
If I have 10 oranges, then I'm thinking that I can get information about a particular orange with:

http://myfruitstand.com/fruits/oranges/3

But is the above url RESTful--don't I need an id between 'fruits' and 'oranges' to conform to REST standards? Or is this url with 'fruits' being immediately followed with the subclass 'oranges' okay?

Question 2:
Similarly, if I want to have a discussion forum about my oranges (not a particular orange), can I put it here:

http://myfruitstand.com/fruits/oranges/comments

Again, is the above url RESTful since there's no id between 'oranges' and 'comments' (an id here, of course, would imply a discussion about a particular orange and I don't want that)? Here, there's no subclass rational of 'oranges' being immediately followed by 'comments.'

thanks in advance, Chuck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

街角卖回忆 2024-10-15 18:56:18

通过资源集合/id 对构建会更加 RESTful,例如 /fruits/:id1/items/:id2,其中 id1 = Orange,id2 = 3

/fruits/orange/items/3

那么如果您丢失了第二个问题的答案也是正确的橙色上的“s”,因为您将橙色视为资源的实例而不是资源集合。

/fruits/orange/comments

例如http://www.slideshare.net的第48张幻灯片/Wombert/phpnw10-designing-http-services-and-restful-interfaces

It would be more RESTful to construct by resource-collection/id pairs, e.g. /fruits/:id1/items/:id2, where id1 = orange, id2 = 3

/fruits/orange/items/3

Then the answer to the second question would be also correct if you lose the 's' on oranges, as you are treating orange as an instance of a resource and not a resource collection.

/fruits/orange/comments

e.g. slide 48 of http://www.slideshare.net/Wombert/phpnw10-designing-http-services-and-restful-interfaces

你在看孤独的风景 2024-10-15 18:56:18

我自己刚才一直在问有关 RESTful 风格的问题。但我的看法是:

如果是水果店,不是就暗指“水果”吗?简单地说:

/oranges/{id}

或者:

/fruits/{id}

这只是橙色类型的水果。

/fruits?type=orange 可以返回橙子列表

Just been asking questions about RESTful style myself. But my take on this is:

If it's a fruit store, isn't 'fruit' implied? How about simply:

/oranges/{id}

Alternatively:

/fruits/{id}

Which is just a fruit of type Orange.

/fruits?type=orange could return you a list of oranges

甜宝宝 2024-10-15 18:56:18

是的,只要是确定性的,所有的 url 都是安全的。 Restful 更多的是关于您正在使用的 http 方法(DELETE 例如删除,POST 编辑,GET 读取,...)。

Yes all urls are restful as long as they are deterministic. Restful is more about the http-methods you are using (DELETE for instance to delete, POST to edit, GET to read, ...).

迷你仙 2024-10-15 18:56:18
  1. 从你对用法的解释来看,我不认为你需要水果资源的 ID。
  2. 同样,第二个也不需要 id。

只要您的网址对您的对象组织进行建模,那么我相信您就可以开始了。

另外,我个人的感觉是,如果您发现需要稍微偏离任何规范才能满足您的应用程序需求,那么这是合理的做法。不要让这种“模型”的坚持妨碍工作的完成。例如,在 RESTful 上下文中,有时您执行与某些对象相关的操作,这些对象并不明确属于 DELETE/PUT/POST/GET 动词。为了保持完美的休息,你会把这些东西塞进去吗?

我现在躲在桌子后面,以免被那些安宁的大师/宗教主义者打昏,他们现在不可避免地会殴打我:)

  1. I don't believe from your explanation of usage that you need an id for the fruit resource.
  2. Similarly no id needed for the second one either.

As long as your urls model your object organization then I believe you're good to go.

Also, my personal feeling is that if you find a case where you need to depart ever so slightly from any specification to meet your application needs, that's a reasonable thing to do. Do not allow adherence to this sort of 'model' interfere with getting the job done. For example in the RESTful context, sometimes you perform actions related to some objects that just don't fall clearly under the DELETE/PUT/POST/GET verbs. Do you smoosh those things into there in order to maintain perfect RESTfulness?

I am now hiding behind my desk so as not to be knocked unconscious by the RESTful gurus/religionists that will inevitably beat up on me now :)

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