0

我有这样的要求,即根据 id 从数据库中获取数据。该记录可以具有相同 id 和类型的多个值,因为它是历史类型。然而,我们使用的版本每次都会改变。我想要的和我得到的样本如下。我正在使用Java8,rest-apijson api在其中使用注释标记关系属性。此数据是从数据库中保存和获取的,因此要成为完全唯一的行,id 和版本组合应该是唯一的。这意味着数据库中存在具有相同 id 但版本不同的记录

我想要的是:

{
  "data":{
        "id": "123",
        "attributes":{
             "name": "SJ"
         },
         "relationships":{
            "contact":{
                    "data":[
                            {
                            "type":"test",
                            "id":"4567"
                             },
                             {
                              "type":"test",
                               "id":"4567"
                             }
                      ]
             }
          }

  },
"included":[
          {
           "type":"test",
           "id":"4567",
           "attributes":{
                  "testfield":"testfield 1",
                  "version": 0
            }
          },
          {
           "type":"test",
           "id":"4567",
           "attributes":{
                    "testfield":"testfield 2",
                    "version":1
             }
          }
  ]
}

我得到什么:

{
  "data":{
        "id": "123",
        "attributes":{
             "name": "SJ"
         },
         "relationships":{
            "contact":{
                    "data":[
                            {
                            "type":"test",
                            "id":"4567"
                             },
                             {
                              "type":"test",
                               "id":"4567"
                             }
                      ]
             }
          }

  },
"included":[
          {
           "type":"test",
           "id":"4567",
           "attributes":{
                  "testfield":"testfield 1",
                  "version":1
            }
          }
  ]
}
4

1 回答 1

1

您不能通过type和的相同组合引用两个不同的对象id

在给定的 API 中,每个资源对象的类型和 id 对必须标识一个唯一的资源。

https://jsonapi.org/format/#document-resource-object-identification

对于版本化资源,您可能希望通过将资源的标识符与版本号相结合来为每个版本生成唯一的 ID。

于 2021-03-23T12:51:47.650 回答