我認爲關鍵是評論是否被帖子資源「包含」。請記住,RESTful網址應該是永久鏈接,因此在所有情況下,特定評論的終點不得更改。它聽起來像是被包含的,所以url模式可以在帖子內嵌入註釋。如果情況並非如此(例如,評論可能會轉移到另一個帖子,如果嵌套會改變url),那麼您需要一個更加扁平化的結構,其中/ comment/{id}網址由帖子資源引用)。
關鍵是如果它是一個RESTful「超媒體API」,那麼它就像它不斷鏈接到嵌套或引用資源的網絡。它不依賴於客戶必須瞭解REST模式或特定知識,以瞭解哪些終點持有引用或包含的資源。
http://blog.steveklabnik.com/posts/2012-02-23-rest-is-over
如果 '註釋' 是資源(S)下一個 '後' 資源:
([httpVerb]/URL)
收到信息:
[get] /posts/{id}
body has a couple options - either it contains the full deep comments array
(depends on how much data, chat pattern)
{
id:xxx,
title:my post,
comments: [...]
}
... or it just contains the post resource with a url reference to the comments e.g.
{
id: xxx,
title: my post,
commentsUrl: /posts/xxx/comments
}
could also have an option like this (or other options to control depth):
[get] /posts/{id}?deep=true
在一篇文章中獲得評論集:
[get] /posts/{id}/comments
returns 200 and an array of comments in the response body
創建註釋的一個帖子:
[post] /posts/{id}/comments
body contains json object to create
returns a 201 created
編輯下發表評論:
[patch|post] /posts/{id}/comments/{id}
body contains json object with subset of fields/data to update
returns a 200
更換後:
[put] /posts/{id}/comment/{id}
body contains json object to *replace*
returns a 200
如果你有一大堆的每發表評論,你也可以考慮尋呼模式:
{
id: xxx,
title: myPost,
pages:6,
commentsUrl:/posts/xxx/comments/page/1
}
然後:
/posts/{id}/comments/pages/{pageNo}
{
nextPage: /posts/xxx/comments/2,
pages:7,
comments:
[ { ...,...,}]
}
每一頁將引用下一個頁面,頁面計數和用於該頁面的評論的陣列。如果你使用了分頁模式,那麼數組中的每個註釋都會有一個參考網址給個人評論。
如果您發現自己在url中添加了一個動作,那麼您可能會做錯某些事情。好讀:http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http
你最終選擇了什麼? – bryanmac