【问题标题】:What is the best RESTful uri for a resource's tags资源标签的最佳 RESTful uri 是什么
【发布时间】:2014-04-21 16:19:02
【问题描述】:

假设你有一个食谱资源,一个食谱可以有标签。然后,您想获取所有食谱中使用的所有标签的列表,URI 是什么?

/recipes/tags

似乎它可能有效,但它打破了不指向特定 id 的约定,例如:

/recipes/1/tags

你也可以使用:

/tags

但我只想要食谱的标签而不是任何其他资源。那么我会使用查询参数吗,例如:

/tags?type-recipes

而 FTR,标签仅用于食谱而不是任何其他资源,因此使用查询参数似乎会产生误导,因为它永远不会是食谱

【问题讨论】:

  • 第一种是最好的方式,不需要指向具体的id,指向一个资源即可。

标签: rest uri


【解决方案1】:

过滤器很棘手——尤其是当您的过滤器也是一种资源时。

您说 /recipes/tags 有问题是正确的。您从 /resouce/{resouceId} 的路径变量转到 /resource/{filter} ,这可能会打开一个痛苦的词。

因此,对于您的第一个示例,请选择

 /recipes?tags={tags to filter on}
 Returns a list of Recipes based on the tag

问题是您没有任何查找方法可以找到可接受的标签。我希望 /tags 返回一个标签列表,然后像这样使用它

 /tags
 Returns a list of all tags

 /tags/{tagID}
 Returns meta data about the specific tag (what the tag is used for, which is superfluous in this case if you only have recipe tags, but it is more flexible)

 /tags/{tagID}/recipes
 Returns a list of recipes associated with that ID.

然后你必须决定是否要支持这样的链接资源

 /tags/eggs/recipes/1
 Recipe details 

这仍然与

相同
/recipes/1 

链接它们是可以接受的——如果 recipe/2 中没有任何鸡蛋并且您尝试使用 /tags/eggs/recipes/2 访问它,这可能会让人感到困惑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多