简体   繁体   English

URI中的REST父ID

[英]REST parent id in URI

Let's say my model consists of a Parent entity that refers some Child entities, via children property. 假设我的模型由一个Parent实体组成,该实体通过children属性引用一些Child实体。 The path segment of the URI of a specific Child , according to REST principles, would be /parent/{parentId}/children/{childId} . 根据REST原则,特定Child的URI的路径段将是/parent/{parentId}/children/{childId}

When performing update operations on a Child , usually childId is all I need in order to uniquely identify the correct Child , deeming the parentId segment in the path redundant. Child执行更新操作时,通常只需要childId就可以唯一地标识正确的Child ,从而认为路径中的parentId段是多余的。 This redundancy aggravates as the hierarchy grows complex. 随着层次结构变得复杂,这种冗余会加剧。

Now that I think of it, it may also cause unexpected behavior: accessing URIs with the same childId but with different parentId could result in the same behavior, if the programmer isn't aware. 现在我想到了它,它也可能导致意外行为:如果程序员不知道,访问具有相同childId但具有不同 parentId URI可能会导致相同的行为。 What should probably happen when accessing a Child under an unrelated Parent is that a client error code should be returned. 在不相关的Parent下访问Child时可能发生的情况是应该返回客户端错误代码。

Currently I think that maybe no hierarchy should be introduced to REST API unless it is absolutely intuitive, as it: 目前我认为可能没有应该向REST API引入层次结构,除非它非常直观,因为它:

  1. Makes the URI - thus the API - more complex. 使URI(因此API)更复杂。 Hardens maintainability. 加强可维护性。
  2. The redundancy may cause users to reason about the outcome of accessing some URIs. 冗余可能导致用户推断访问某些URI的结果。
  3. The redundancy may become a pitfall to the unaware programmer. 冗余可能成为不知情的程序员的陷阱。

Is there a way to evade this redundancy and still conform REST principles? 有没有办法逃避这种冗余并仍然符合REST原则?

Yes, just structure your URL like this... 是的,只需像这样构建你的网址......

/children/{childId}

Since you can infer the parent from the child on the server side, there is no reason to declare it in the URL. 由于您可以从服务器端的子项推断父项,因此没有理由在URL中声明它。 You should only put multiple resources in a URL when you absolutely need to. 在绝对需要时,您应该只在URL中放置多个资源。 For example, a user voting on a comment. 例如,用户对评论进行投票。 Since there is no formal way to determine on the database side, you would create a url like.. 由于在数据库方面没有正式的方法来确定,你可以创建一个类似的URL。

/voter/{userId}/comment/{commentId}/upvote

for me this question answer is going to be driven by data modeling, and for data modeling the question is "using composite key for child model or not" if he is using composite key so you have to put the parent id in URI if not and child have its own id which is not composite so you can just use URL with child id only. 对我来说,这个问题的答案将由数据建模驱动,对于数据建模,如果他使用复合键,那么问题是“使用复合键是否为子模型”,因此如果不是,则必须将父ID放在URI中。 child有自己的id,它不是复合的,所以你只能使用带有child id的URL。

so this is data modeling question!!! 所以这是数据建模问题!!!

Since REST is referring to Resources, they don't necessarily have to be hierarchical. 由于REST指的是资源,因此它们不一定必须是分层的。

In a similar API, I have Sections, Categories, and Articles. 在类似的API中,我有章节,类别和文章。 Each of these is, of course, under one of the other, but in REST I specify them as section/{id} & article/{id}. 当然,其中每个都在另一个之下,但在REST中我将它们指定为section / {id}&article / {id}。 They're still links to individual resources - but since they CAN be independent of their parent, the hierarchy isn't important to specify in the URI. 它们仍然是指向各个资源的链接 - 但由于它们可以独立于其父级,因此在URI中指定层次结构并不重要。

If you definitely want to specify the hierarchy in the URI, you should check to make sure the parent is the parent of the child, and throw an error elsewise - in order to maintain hierarchical integrity. 如果您肯定要在URI中指定层次结构,则应检查以确保父项是子项的父项,并在其他方面抛出错误 - 以保持层次结构的完整性。

TL;DR TL; DR

  • /parent/{parentID} /父母/ {}的parentID
  • /child/{childID} /儿童/ {} childID的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM