Rest API 多对多关系的资源设计
Rest API resource design for many-to-many relation
Post 可以在许多 类别 和 部分 中。
Category and Section can have many Posts
要列出 sections/categories 使用:
GET /posts/categories
GET /posts/sections
似乎比以下设计更好:
GET /categories
GET /sections
但是如何从sections/categories请求Posts?
这似乎很尴尬(或者可能不是?):
GET /posts/sections/{id}/posts
这些可能会有问题:
GET /posts?section={id}
因为我已经有几个过滤器,所以我以:
GET /posts?section={id}&filter1={f1}&filter2={f2}....
有什么建议吗?
我个人会使用查询参数方法来过滤帖子集合,因为它是多对多关系并且可以独立管理资源:
GET /posts?section={id}
我可能会对类别和部分使用以下映射:
GET /categories
GET /sections
例如,如果一组帖子仅属于一个 单个 类别,我将使用:
GET /categories/{id}/posts
Post 可以在许多 类别 和 部分 中。 Category and Section can have many Posts
要列出 sections/categories 使用:
GET /posts/categories
GET /posts/sections
似乎比以下设计更好:
GET /categories
GET /sections
但是如何从sections/categories请求Posts?
这似乎很尴尬(或者可能不是?):
GET /posts/sections/{id}/posts
这些可能会有问题:
GET /posts?section={id}
因为我已经有几个过滤器,所以我以:
GET /posts?section={id}&filter1={f1}&filter2={f2}....
有什么建议吗?
我个人会使用查询参数方法来过滤帖子集合,因为它是多对多关系并且可以独立管理资源:
GET /posts?section={id}
我可能会对类别和部分使用以下映射:
GET /categories
GET /sections
例如,如果一组帖子仅属于一个 单个 类别,我将使用:
GET /categories/{id}/posts