简体   繁体   English

树形结构的REST API样式

[英]REST API style for tree structure

I have items in tree structure. 我有树状结构的物件。 Each Item has Id and ParentId . 每个Item都有IdParentId

To get Item by Id I use items/:id 要通过Id获取Item ,我使用items/:id

What is the best way to create endpoint for GetItemsByParentId(parentId) method? GetItemsByParentId(parentId)方法创建端点的最佳方法是什么?

You would rather ask a person "Who are your children" than "Who has you as parent", wouldn't you? 您宁愿问一个人“谁是您的孩子”,而不是问“您有谁作为父母”,不是吗? That said I suggest to use /items/{id}/children . 也就是说,我建议使用/items/{id}/children

The response you get from the server usually contains the appropriate link in that case: 在这种情况下,您从服务器获得的响应通常包含相应的链接:

{
  links: {
    self: "http://foo/items/2",
    children: "http://foo/items/2/children"
  },
  ...
}

Your API could also support embedding children on demand. 您的API还可以支持按需嵌入子级。 Something like http://foo/items/2?embedChildren or a different media type in the Accept header could then return: 然后,诸如http://foo/items/2?embedChildrenAccept标头中的其他媒体类型可能会返回:

{
  links: {
    self: "http://foo/items/2",
    children: "http://foo/items/2/children"
  }
  children: [
   ...
  ],
  ... 
} 

Including the children link in that case too is a good idea. 在这种情况下也包括children链接是一个好主意。 That way you can still add children posting to that URL. 这样,您仍然可以添加发布到该URL的子级。

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

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