如何创建复合文档?

Jer*_*oef 18 json-api

我正在考虑使用JSONAPI标准来设计我们的API.这个API必须能够做的一件事是接受一个复合文档(深层几层)并创建它.根对象拥有所有后代('to-many'关系),服务器在那一点上什么都不知道,所以客户端不可能提供id.

这是规范支持还是客户端必须按顺序为文档中的每个对象发出http请求?

equ*_*nt8 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"
    }
  }]
}
Run Code Online (Sandbox Code Playgroud)

  • 这假定您知道要创建的资源的ID,放入关系和包含的文档中.虽然我认为您可以为未知ID赋予特殊值,并让服务器创建它们. (3认同)