【问题标题】:JSON API: Correct way to show links?JSON API:显示链接的正确方法?
【发布时间】:2017-02-21 17:18:01
【问题描述】:

我使用 Laravel 和 Laravel 5 JSON API Transformer package 制作了一个 JSON API,它在 jsonapi.org 上列出

目前,一切都按预期工作,来自我的 api 的示例响应就像(顺便说一句,验证失败,所以我查看了 jsonapi 规范):

{
  "data": [
    {
      "type": "inventory",
      "id": "INV0001",
      "attributes": {
        "inv_inventory_id": "INV0001",
        "inv_owner_company_id": 1,
        "inv_owner_department_id": 1,
        "inv_user_department_id": 1,
        "inv_user_worker_id": 1,
        "title": "Schreibtisch"
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/inventory/INV0001"
        },
        "user": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1"
        },
        "owner_dept": {
          "href": "http://127.0.0.1:8000/api/v2/department/1"
        },
        "owner_comp": {
          "href": "http://127.0.0.1:8000/api/v2/company/1"
        }
      },
      "relationships": {
        "worker": {
          "data": {
            "type": "worker",
            "id": "1"
          }
        },
        "department": {
          "data": {
            "type": "department",
            "id": "1"
          }
        },
        "company": {
          "data": {
            "type": "company",
            "id": "1"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "worker",
      "id": "1",
      "attributes": {
        "wrk_forename": "Moritz",
        "wrk_surname": "ASDF",
        "wrk_department_id": 2,
        "wrk_homeoffice": true,
        "wrk_room_id": 1
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1"
        },
        "hardware": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/hardware"
        },
        "software": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/software"
        },
        "inventory": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/inventory"
        },
        "accessory": {
          "href": "http://127.0.0.1:8000/api/v2/worker/1/accessory"
        }
      }
    },
    {
      "type": "department",
      "id": "1",
      "attributes": {
        "department": "Entwicklung",
        "dept_floor_id": 3
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/department/1"
        },
        "floor": {
          "href": "http://127.0.0.1:8000/api/v2/floor/3"
        },
        "hardware": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/hardware"
        },
        "software": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/software"
        },
        "inventory": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/inventory"
        },
        "accessory": {
          "href": "http://127.0.0.1:8000/api/v2/department/1/accessory"
        }
      }
    },
    {
      "type": "company",
      "id": "1",
      "attributes": {
        "company": "GermanPersonnel",
        "com_building_id": 1
      },
      "links": {
        "self": {
          "href": "http://127.0.0.1:8000/api/v2/company/1"
        }
      }
    }
  ],
  "links": {
    "self": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    },
    "first": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    },
    "last": {
      "url": "http://127.0.0.1:8000/api/v2/inventory?page[number]=1&page[size]=10"
    }
  },
  "meta": {
    "page": {
      "total": 1,
      "last": 1,
      "number": 1,
      "size": 10
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

但是根据jsonapi.org 的规范,链接应该是这样的

  "links": {
    "self": "http://127.0.0.1:8000/api/v2/inventory/INV0001"
   },

我的问题是:

将我的示例输出中的链接显示为对象是否合法 用“href”?我很困惑,因为我使用的包是 在 jsonapi.org 上列出,但似乎不符合规范。

顺便说一句:我的英语可能有点混乱,但我希望我能尽可能地描述我的问题

【问题讨论】:

  • 我从未见过 API 使用 href 来表示链接。通常它就像foo_url: "http://www.coolapi.com/api/v3/foo/12"
  • 所以将链接显示为字符串是正确的方式?

标签: specifications json-api


【解决方案1】:

这实际上是基于规范的有效 JSON API 输出,

http://jsonapi.org/format/#document-links

如果指定,链接成员可用于表示链接。每个链接成员的值必须是一个对象(“链接对象”)。

规范中的有效样本

"links": {
  "related": {
    "href": "http://example.com/articles/1/comments",
    "meta": {
      "count": 10
    }
  }
}

链接对象的每个成员都是一个“链接”。链接必须表示为:

  • 包含链接 URL 的字符串。
  • 可以包含以下成员的对象(“链接对象”):
    • href:包含链接 URL 的字符串。
    • 元:元对象,包含有关链接的非标准元信息。

因此,您的输出实际上是有效的。

【讨论】:

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