什麼是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. Opencart SEF遺址相關鏈接
- 3. 如何在使用相關網址時使用相關網址
- 4. 與JSON鏈接有很多關係
- 5. HTML鏈接關係
- 6. 亞馬遜API網關的.NET核心vs實施自己的API網關?
- 7. Django相關網址和https
- 8. 加入相關網址?
- 9. Xampp的相關網址
- 10. Makefile自動鏈接依賴關係?
- 11. Katharsis壓制關係鏈接?
- 12. 我如何識別關係= NoFOLLOW鏈接
- 13. 騾子API網關Vs的API經理
- 14. Android鏈接網址鏈接非網址
- 15. 鏈接上的CakePHP關係
- 16. 關係鏈接表laravel
- 17. PDF中的相關鏈接
- 18. Adobe AEM相關鏈接
- 19. Kohana鏈接相關問題
- 20. tvml中的相關鏈接?
- 21. keystoneJS關係自我
- 22. 自我M:N關係
- 23. 在我的網站中生成不相關的鏈接
- 24. 間接關係相互
- 25. WSO2 API網關將JSON轉換爲JSON
- 26. 我如何專注鏈接元素(相關鏈接)
- 27. 關係Laravel API
- 28. API網關vs Mule服務器
- 29. MongoDB關係vs SQL
- 30. Gradle依賴關係配置:實現vs api vs runtimeonly vs compileonly