我正在考虑使用JSONAPI标准来设计我们的 API。这个 API 必须能够做的一件事是接受一个复合文档(深几层)并创建它。根对象拥有服务器当时一无所知的所有后代(“对多”关系),因此客户端不可能提供 id。
规范是否支持这一点,还是客户端必须按顺序为文档中的每个对象发出 http 请求?
来自 http://jsonapi.org/format/#document-compound-documents
复合文档需要“完全链接”,这意味着每个包含的资源必须由同一文档中的至少一个资源标识符对象标识。这些资源标识符对象可以是主要数据或表示包含在主要或包含的资源中的资源链接。完整链接要求的唯一例外是通过稀疏字段集排除原本包含链接数据的关系字段。
{
"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!"
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}