假设我已经构建了一个 REST 服务来做笔记,看起来像这样:
GET /notes/ // gives me all notes
GET /notes/{id} // gives the note with the identified by {id}
POST /notes/ // creates note
PUT /notes/{id} // updates note identified by {id}
DELETE /notes/{id} // delete note
现在我想创建允许我获取/更新/删除第一个或最后一个笔记的子资源。
GET /notes/first // get first note added
PUT /notes/last // updates last note added
DELETE /notes/first // delete first note
这是否违背了 REST 的原则?
提前谢谢你:)
编辑:
PS:在这个例子中,我希望 ID 总是一个数字
最佳答案
在我看来,这是完全合法且可取的。我特别喜欢 DELETE/notes/first
的语义,但也考虑了 POST/notes/first
- 在第一个笔记之前放一个新笔记 .
在您的情况下,first
和 last
是某种特殊的魔术标识符或占位符。另一方面,有哪些替代方案?
关于web-services - 创建 REST 子资源来获取列表的最后一个或第一个元素是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9380833/