我正在考虑使用JSONAPI标准来设计我们的API。这个API必须能够做的一件事是接受一个复合文档(深层几层)并创建它。根对象拥有所有后代('to-many'关系),服务器在那时就什么都不知道,所以客户端不可能提供id。
规范是否支持或客户端是否必须按顺序为文档中的每个对象发出http请求?
答案 0 :(得分:5)
来自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"
}
}]
}