假设我正在为狗及其主人制作一个 RESTful API。我有一个狗端点 /dogs/123
和所有者端点 /owner/321
。
如果我想列出主人的狗,我可以获取 /owner/321/dogs
或 /dogs?owner_id=321
。
在哪种情况下哪个更可取?我应该支持这两个选项吗?
最佳答案
如本 best practices reference card 中所述:
You should leverage the hierarchical nature of the URL to imply structure (aggregation or compositon)
你可以选择:
GET /owners/321/dogs
关于api - 何时选择关联而非查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035556/