我有树结构中的项目。每个 Item
都有 Id
和 ParentId
。
要通过 Id
获取 Item
,我使用 items/:id
为 GetItemsByParentId(parentId)
方法创建端点的最佳方式是什么?
最佳答案
你宁愿问一个人“你的 child 是谁”,而不是“你作为 parent 是谁”,不是吗?也就是说,我建议使用 /items/{id}/children
。
在这种情况下,您从服务器获得的响应通常包含相应的链接:
{
links: {
self: "http://foo/items/2",
children: "http://foo/items/2/children"
},
...
}
您的 API 还可以支持按需嵌入子项。然后,http://foo/items/2?embedChildren
或 Accept
header 中的其他媒体类型可能会返回:
{
links: {
self: "http://foo/items/2",
children: "http://foo/items/2/children"
}
children: [
...
],
...
}
在这种情况下包含 children
链接也是一个好主意。这样您仍然可以将 child 帖子添加到该网址。
关于javascript - 树结构的 REST API 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707992/