创建 REST 子资源来获取列表的最后一个或第一个元素是不好的做法吗? [英] Is creating REST subresources to get the last or first element of a list bad practice?

查看:33
本文介绍了创建 REST 子资源来获取列表的最后一个或第一个元素是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经构建了一个用于做笔记的 REST 服务,看起来像这样:

Let's say I have built a REST service for making notes that looks something like this:

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

现在我想创建子资源,允许我获取/更新/删除第一个或最后一个注释.

now I want to create subresources that allows me to get/update/delete the first or last note.

GET    /notes/first // get first note added
PUT    /notes/last  // updates last note added
DELETE /notes/first // delete first note

这是否违背了 REST 的原则?

Does this go against the principles of REST?

先谢谢你:)

PS:在这个例子中,我希望 ID 总是一个数字

PS: In this example i expect the ID to be always a number

推荐答案

在我看来,这是完全合法和可取的.我特别喜欢DELETE/notes/first的语义,但也考虑了POST/notes/first - 在第一个之前放一个新的笔记.

In my humble opinion this is perfectly legal and desirable. I especially like the semantics of DELETE /notes/first, but also consider POST /notes/first - put a new note before the first one.

在你的情况下 firstlast 是某种特殊的魔法标识符或占位符.另一方面,有哪些替代方案?

In your case first and last are some sort of special magic identifiers or placeholders. On the other hand what are the alternatives?

这篇关于创建 REST 子资源来获取列表的最后一个或第一个元素是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆