0

除了我的最后一个问题ember.js JSONAPIAdapter with hasMany,一位同事询问是否可以像这样嵌入工作 JSON:API 结构中的“种类”-sideloaded 关系:

{
    "data": [
      {
        "type": "altersgruppe",
        "id": "1",
        "attributes": {
          "name": "UNTER_21"
        },
        "relationships": {
          "tarifbeitraege": {
            "data": [
              {
                "type": "tarifbeitrag",
                "id": "3",
                "attributes": {
                  "name": "ZAHN70",
                  "beitrag": "3-29,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              },
              {
                "type": "tarifbeitrag",
                "id": "4",
                "attributes": {
                  "name": "ZAHN90",
                  "beitrag": "4-28,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "30.99"
                }
              }
            ]
          }
        }
      },
      {
        "type": "altersgruppe",
        "id": "2",
        "attributes": {
          "name": "ALTER_21_24"
        },
        "relationships":{
          "tarifbeitraege": {
            "data": [
              {
                "type": "tarifbeitrag",
                "id": "1",
                "attributes": {
                  "name": "ZAHN70",
                  "beitrag": "1-25,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              },
              {
                "type": "tarifbeitrag",
                "id": "2",
                "attributes": {
                  "name": "ZAHN90",
                  "beitrag": "2-25,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              }]
          }
        }
      }
    ]
}

其背后的想法:我们可以在 java 后端(侧载结构更难实现)中使用问题较少的关系。

但是上面的 JSON 结构不起作用。store 仅包含第一级数据,即“altersgruppe”,但“tarifbeitraege”为空。

4

1 回答 1

1

这种类型的文档在 JSON:API 规范中称为复合文档。

复合文档的“关系”部分应该只有关系——那里的单个对象应该是资源标识符对象。把属性放在那里是行不通的,因为那不是它们应该在的地方。

相反,完整的对象被侧载在顶级“包含”部分中。因此,您的响应应该看起来更像这样:

{
    "data": [
      {
        "type": "altersgruppe",
        "id": "1",
        "attributes": {
          "name": "UNTER_21"
        },
        "relationships": {
          "tarifbeitraege": {
            "data": [
              { "type": "tarifbeitrag", "id": "3" },
              { "type": "tarifbeitrag", "id": "4" }
            ]
          }
        }
      },
      {
        "type": "altersgruppe",
        "id": "2",
        "attributes": {
          "name": "ALTER_21_24"
        },
        "relationships":{
          "tarifbeitraege": {
            "data": [
              { "type": "tarifbeitrag", "id": "1" },
              { "type": "tarifbeitrag", "id": "2" }
              ]
           }
        }
      }
    ],
    "included": [
      {
        "type": "tarifbeitrag",
        "id": "3",
        "attributes": {
          "name": "ZAHN70",
          "beitrag": "3-29,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "4",
        "attributes": {
          "name": "ZAHN90",
          "beitrag": "4-28,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "30.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "1",
        "attributes": {
          "name": "ZAHN70",
          "beitrag": "1-25,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "2",
        "attributes": {
          "name": "ZAHN90",
          "beitrag": "2-25,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      }
    ]
}

http://jsonapi.org的主页上有一个示例,该示例显示了一个包含侧载的示例,以及描述复合文档的规范部分中的一个示例。

于 2015-08-08T17:17:39.257 回答