【问题标题】:HAL JSON - is it allowed by the standard to have multiple levels of embeds?HAL JSON - 标准是否允许有多个级别的嵌入?
【发布时间】:2014-07-03 14:02:11
【问题描述】:

我想知道是否允许在 JSON 中的 _embed 元素中包含更多的 _embed 元素,或者想法是否只有一个级别?

查看普通 HAL 示例:

{
"_links": {
    "self": { "href": "/orders" },
    "curies": [{ "name": "ea", "href": "http://example.com/docs/rels/{rel}", "templated": true }],
    "next": { "href": "/orders?page=2" },
    "ea:find": {
        "href": "/orders{?id}",
        "templated": true
    },
    "ea:admin": [{
        "href": "/admins/2",
        "title": "Fred"
    }, {
        "href": "/admins/5",
        "title": "Kate"
    }]
},
"currentlyProcessing": 14,
"shippedToday": 20,
"_embedded": {
    "ea:order": [{
        "_links": {
            "self": { "href": "/orders/123" },
            "ea:basket": { "href": "/baskets/98712" },
            "ea:customer": { "href": "/customers/7809" }
        },
        "total": 30.00,
        "currency": "USD",
        "status": "shipped"
    }, {
        "_links": {
            "self": { "href": "/orders/124" },
            "ea:basket": { "href": "/baskets/97213" },
            "ea:customer": { "href": "/customers/12369" }
        },
        "total": 20.00,
        "currency": "USD",
        "status": "processing"
    }]
}

}

以及问题是什么(注意第二个嵌入是第一个嵌入的孩子):

{
   "_links" : {
     "self" : {
     "href" : "http://localhost:8090/fs/rest/roles/roleDefinition=Z1407031312513158080GIVMZ"
     }
   },
   "_embedded" : {
      "roles" : [ {
      "_links" : {
          "self" : {
               "href" : "http://localhost:8090/fs/rest/roles/Z1407031312513168080XCAKL"
           }
       },
       "id" : "Z1407031312513168080XCAKL",
        "_embedded" : {
             "roleDefinition" : {
              "_links" : {
              "self" : {
               "href" : "http://localhost:8090/fs/rest/roledefinitions/Z1407031312513158080GIVMZ"
        (rest ommitted)

我自己正在阅读http://stateless.co/hal_specification.html 的文档,因为嵌入式资源与普通资源相同,因此它可以再次包含嵌入式资源?

【问题讨论】:

    标签: json web-services rest hal-json


    【解决方案1】:

    我也遇到过这个问题。似乎答案是肯定或否定。

    因为嵌入的资源可以

    完整、部分或不一致的版本

    ,使用_embedded标签,还是直接嵌入资源,由实现者决定。

    在我看来,最好像使用链接时一样返回嵌入的资源。 api 的使用者可能已经编写了解析器来处理嵌入的资源,(通常也包括 _embedded 标记)

    https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-06#section-4.1.2

    4.1.2。 _嵌入式

    保留的“_embedded”属性是可选的

    它是一个属性名称为链接关系类型的对象(如
    由 [RFC5988] 定义)并且值是资源对象或
    资源对象数组。

    嵌入式资源可能是完整、部分或不一致的版本 从目标 URI 提供的表示形式。

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2023-01-20
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      相关资源
      最近更新 更多