【发布时间】:2020-01-21 16:58:30
【问题描述】:
我正在构建一个 api,我正在尝试处理以下问题:
客户端用application/vnd.api+json发送Accept头,所以我回复JSON:API格式的资源。
例如以下表格:
- 作者
- 文章
具有 1:n 关系 - 例如,一位作者写了 100 篇文章。
我的目标是:
获取/作者
返回作者(资源对象)定义数量的文章(关系资源对象) - 例如他写的前 3 篇文章。
采用这种格式:
{
"links": {
"self": "http://example.com/author",
"next": "http://example.com/author?page[offset]=2",
"last": "http://example.com/author?page[offset]=10"
},
"data": [
{
"type": "author",
"id": "1",
"attributes": {
"name": "Arthur"
},
"relationships": {
"article": {
"count": 100,
"links": {
"self": "http://example.com/author/1/relationships/article",
"related": "http://example.com/author/1/article"
},
"data": [
{
"type": "article",
"id": "4",
"attributes": {
"title": "1 reason why I should use json:api"
},
"links": {
"self": "http://example.com/article/4"
}
},
{
"type": "article",
"id": "7",
"attributes": {
"title": "2 reasons why I should use json:api"
},
"links": {
"self": "http://example.com/article/7"
}
},
{
"type": "article",
"id": "42",
"attributes": {
"title": "3 reasons why I should use json:api"
},
"links": {
"self": "http://example.com/article/42"
}
}
]
}
}
}
]
}
我的想法:
使用类似的查询参数
http://example.com/author?relationship[]=article,orderby=date,asc=true,limit=3
tl;博士:
以 JSON:API 格式限制/分页相关资源的正确方法是什么?
【问题讨论】:
标签: rest api json-api jsonapi-resources