用于多次删除的 REST 架构?
我向服务器发送删除请求,如下所示:
@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
对于单个用户删除。但是当多个用户想要删除时该怎么办呢?我想遵守 REST 架构,但我想看看发送多个删除请求的其他方法?
PS:这是一个合适的方式吗:
@RequestMapping(value = "/user", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteUser(HttpServletResponse response, @RequestBody String users) throws IOException {
...
}
I send a delete request to server as like:
@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
for a single user delete. However what to do when multiple users wants to be deleted? I want to obey REST architecture but I want to see the another ways from sending multiple delete requests?
PS: Is this a suitable way:
@RequestMapping(value = "/user", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteUser(HttpServletResponse response, @RequestBody String users) throws IOException {
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为休息是面向资源的拱门。
尝试设计一个代表“多个用户”资源的更高级别的域对象,
并删除该对象。
也许是
Subbu Allamaraju 、 oreilly 的伟大著作《restful web services Cookbook》,谈论这个主题:
since rest is resource oriented arch.
try design a higher level domain object which represents 'multiple users' resource ,
do delete on that object.
maybe
the great book 'restful web services cookbook' by Subbu Allamaraju , oreilly , talk about this topic:
我对 REST 非常熟悉,认识到使用 POST 来实现 DELETE 是对 REST 合规性的背离。我无法确切说明这是一条好规则的确切原因。
我怀疑这可能与 API 不可知工具的可行性有关(例如,开发工具根据动词定义的给定对实现的状态做出假设,而无需配置为了解特定的 API 方法do)或在部分成功删除的情况下无法返回细粒度错误,但这些只是猜测,并不是这个问题的核心。
Swanliu 的答案是指使用表示分组结构的 URL 作为删除的目标,但给出的示例“/users/expired”建议采用固定的(可能是系统定义的)分组。任意集合的更面向用户的情况仍然需要在某些时候进行枚举来实现。
为大小为 N 的组发出 N 个 DELETE 并不具有吸引力,因为复合延迟和缺乏原子性,但 REST DELETE 可能仅针对单个资源。
我认为 Swanliu 的响应所暗示的最佳实践可能是定义一个 POST 操作,该操作能够创建一个资源,该资源成为要删除的对象的新包含父级。 POST 可以返回正文,因此相关系统可以为此非域分组资源创建唯一标识符并将其返回给客户端,客户端可以转身并发出第二个请求来删除它。 POST 创建的资源是短暂的,但却是有目的的——它的消亡级联到作为批量删除操作所需目标的域对象。
诚然,两个网络交换比一个网络交换更不可取,但考虑到较小的批量删除是两个对象,并且需要两个 DELETE 操作来解决,无论如何,这似乎是一个公平的权衡 - 想想看,就像您得到了每个对象一样第二个对象之外的对象是免费的。
I'm familiar enough with REST to recognize that using a POST to achieve a DELETE is a deviation from REST compliance. I'm not able to put my finger on the precise reason that's a good rule.
I suspect it may have something to do with either the feasibility of API-agnostic tooling (e.g. a dev tool that makes so assumptions about the state of an implementation based on givens about the verb definitions without needing to be configured to understand what specific API methods do) or the inability to return fine grained errors in the event of a partially successful delete, but those are just guesses and not really central to this question.
Swanliu's answer refers to use of URLs that represent a grouping construct as the target of a delete, but the example given, "/users/expired", suggests a fixed (and possibly system-defined) grouping. The more user-oriented case of an arbitrary collection still requires an enumeration at some point to achieve.
Issuing N DELETEs for a group of size N is not attractive, both because of the compound latency and lack of atomicity, but a REST DELETE may only target a single resource.
I think the best-practice implied by Swanliu's response might be to define a POST operation capable of creating a resource that becomes the new containment parent of the objects to delete. A POST can return a body, so the system in question can create manufacture a unique identifier for this non-domain grouping resource and return it to the client, which can turn around and issue a second request to DELETE it. The resource created by the POST is short-lived, but purposeful--it's demise cascades to the domain objects that were the desired target of a bulk delete operation.
Granted, two network exchanges is less desirable than just one, but given that the smaller bulk delete is two objects and would require two DELETE operations to address without anyhow, it seems like a fair tradeoff--think of it like you're getting every object beyond the second one free.
我对 REST 的理解是,这正是你必须做的。如果有替代方案,我也很想知道:)
如果您不想发送多个删除请求,那么您必须设计一个更粗糙的 api。 (这就是为什么大多数 API 都是 RESTful,而不是 REST)
顺便说一句,我认为您需要 RequestMethod.DELETE?
My understanding of REST is that is exactly what you must do. If there is an alternative I would love to know too :)
If you don't want to send multiple delete requests, then you have to design a more coarse api. (this is why most APIs out there are RESTful, not REST)
Btw, I think you need RequestMethod.DELETE?
AFAIK 基本的 REST 用于处理单个资源。我将使用
POST
发送到/user/
资源,并使用包含要删除的 ID 的特殊deleteUsers
正文。AFAIK the basic REST is for working with a single resource. I would go with a
POST
to the/user/
resource and a specialdeleteUsers
body containing the IDs to delete.