【发布时间】:2015-08-22 15:29:08
【问题描述】:
我正在考虑使用JSONAPI 标准来设计我们的API。这个 API 必须能够做的一件事是接受一个复合文档(深几层)并创建它。根对象拥有服务器当时一无所知的所有后代(“对多”关系),因此客户端无法提供 id。
规范是否支持这一点,还是客户端必须按顺序为文档中的每个对象发出 http 请求?
【问题讨论】:
标签: json-api
我正在考虑使用JSONAPI 标准来设计我们的API。这个 API 必须能够做的一件事是接受一个复合文档(深几层)并创建它。根对象拥有服务器当时一无所知的所有后代(“对多”关系),因此客户端无法提供 id。
规范是否支持这一点,还是客户端必须按顺序为文档中的每个对象发出 http 请求?
【问题讨论】:
标签: json-api
来自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"
}
}]
}
【讨论】: