【发布时间】:2016-06-29 17:58:03
【问题描述】:
在构建 API 端点之前,我正在阅读 this。我读到了这个关于复合文档的引用:
为了减少 HTTP 请求的数量,服务器可以允许响应 包括相关资源以及请求的主要资源 资源。此类响应称为“复合文档”。
以下是使用 JSON API 规范的示例 JSON 响应:
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "http://example.com/articles/1"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
因此,据我所知,关系部分提供了有关文章表与其他表之间关联的基本/稀疏信息。它看起来像是一篇文章属于_一位作者并拥有_许多 cmets。
链接的用途是什么? API 是否必须使用该链接才能接收有关该关系的更详细的 JSON?这不需要额外的 API 调用吗?这效率高吗?
“包含”部分似乎包含有关关系/关联的更详细信息?
“包含”和“关系”都需要吗?需要这两个部分的直觉是什么?
【问题讨论】:
标签: ruby-on-rails json json-api