什麼是self
鏈接和related
鏈接的JSON API是什麼?JSON API自我vs相關鏈接關係網址
回答
我很困難找到一個直接,可讀的答案,我實際返回的響應。
對於以JSON API的例子,他們有以下JSON:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
這條線的位置:
"self": "http://example.com/articles/1/relationships/author"
是一個 「關係鏈接」
這條線的位置:
"self": "http://example.com/articles/1/relationships/comments"
又是一個「關係鏈接」
是的,我知道這是混亂的,因爲另外一個叫related
。這些鏈接的目的是什麼?目的是只有管理關係。所以說你做GET /articles/1/relationships/comments
你做不是返回評論的信息。你只需要返回一個資源類型/ ID和一些其他的東西,如元數據和鏈接的數組。例如:
{
"data": [{
"type": "comments",
"id": "13"
}, {
"type": "comments",
"id": "29"
}],
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"next": "http://example.com/articles/1/relationships/comments?page[offset]=2",
"last": "http://example.com/articles/1/relationships/comments?page[offset]=4"
},
"meta": {
"copyright": "Copyright 2015 Example Corp.",
"authors": [
"Zach Aysan"
]}
}
爲什麼這很有用?因爲有時候我們想只是刪除關係,而不是資源(評論,作者)本身。因此,例如,如果我們執行的DELETE /articles/1/relationships/author
不會從用戶表中刪除該用戶,則只會將該用戶作爲作者刪除。要刪除只有一些評論,我們做一個PATCH /articles/1/relationships/comments
,只包括我們想保留的評論。但請記住,如果後端認爲這是正確的操作,則可能會刪除實際的評論。 (既然沒有關聯的文章有什麼好評論?)
另一個鏈接呢?爲什麼它是/articles/1/author
而不是/people/9
?因爲文章的作者可能會在請求之間更改,並且GET /articles/1/author
將始終返回當前作者。這就是爲什麼我們通常不需要支持諸如PATCH /articles/1/author
之類的東西,因爲它通常會更有效/更安全地引導資源本身的變化。 PATCH /people/9
,例如,如果某人在編輯文章頁面上更改了他們的頭像。即使管理員更改了文章的作者身份,PATCH
仍然轉到正確的資源。
我知道。這一切都有點乏味,但是一旦理解了Ember Data這樣的東西,就應該將它們組合在一起。
- 1. 鏈接關係
- 2. 鏈接本地地址的相關性?
- 3. Opencart SEF遺址相關鏈接
- 4. 如何在使用相關網址時使用相關網址
- 5. 與JSON鏈接有很多關係
- 6. 乾淨的網址弄亂了相關鏈接
- 7. 自我關係與關係
- 8. Coredata鏈接關係
- 9. HTML鏈接關係
- 10. 亞馬遜API網關的.NET核心vs實施自己的API網關?
- 11. Rails 3域相關網址
- 12. mod_rewrite和相關網址
- 13. 加入相關網址?
- 14. Xampp的相關網址
- 15. Django相關網址和https
- 16. Symfony2:AvalancheImagineBundle和相關網址
- 17. Makefile自動鏈接依賴關係?
- 18. Katharsis壓制關係鏈接?
- 19. 我如何識別關係= NoFOLLOW鏈接
- 20. 騾子API網關Vs的API經理
- 21. Android鏈接網址鏈接非網址
- 22. 關係鏈接表laravel
- 23. 鏈接依賴關係
- 24. 鏈接上的CakePHP關係
- 25. 自我M:N關係
- 26. keystoneJS關係自我
- 27. Neo4j自我關係
- 28. Kohana鏈接相關問題
- 29. 打開相關鏈接
- 30. Adobe AEM相關鏈接