2017-02-14 68 views
2

我做了使用Laravel与Laravel 5 JSON API Transformer package这是上市jsonapi.orgJSON API:正确显示链接的方式?

现在一个JSON API,一切正常,从我的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上列出的 ,但似乎不符合规格。

BTW:我的英语可能是一个有点混乱,但我希望我descriped我的问题以及可能的

+1

我从来没有见过API使用'href'来表示链接。通常它就像'foo_url:“http://www.coolapi.com/api/v3/foo/12”' – jready

+0

因此,以字符串形式显示链接是正确的方法吗? –

回答

1

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

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

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

有效采样从规格

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

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

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

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