用于多次删除的 REST 架构?

发布于 2024-12-05 21:48:19 字数 478 浏览 2 评论 0原文

我向服务器发送删除请求,如下所示:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

煮茶煮酒煮时光 2024-12-12 21:48:19

因为休息是面向资源的拱门。

尝试设计一个代表“多个用户”资源的更高级别的域对象,

并删除该对象。

也许是

@RequestMapping(value = "/users/expired", method = RequestMethod.Delete)

Subbu Allamaraju 、 oreilly 的伟大著作《restful web services Cookbook》,谈论这个主题:

  • 第 11.10 章,如何批量处理类似的资源;
  • 第11.11章,如何触发批量操作;
  • 第11.12章,何时使用post合并多个请求;
  • 第11.13章,如何支持批量请求

since rest is resource oriented arch.

try design a higher level domain object which represents 'multiple users' resource ,

do delete on that object.

maybe

@RequestMapping(value = "/users/expired", method = RequestMethod.Delete)

the great book 'restful web services cookbook' by Subbu Allamaraju , oreilly , talk about this topic:

  • chapter 11.10, how to batch process similar resources;
  • chapter 11.11, how to trigger batch operation;
  • chapter 11.12, when to use post to merge multiple request ;
  • chapter 11.13, how to support batch request
近箐 2024-12-12 21:48:19

我对 REST 非常熟悉,认识到使用 POST 来实现 DELETE 是对 REST 合规性的背离。我无法确切说明这是一条好规则的确切原因。

我怀疑这可能与 API 不可知工具的可行性有关(例如,开发工具根据动词定义的给定对实现的状态做出假设,而无需配置为了解特定的 API 方法do)或在部分成功删除的情况下无法返回细粒度错误,但这些只是猜测,并不是这个问题的核心。

Swanliu 的答案是指使用表示分组结构的 URL 作为删除的目标,但给出的示例“/users/expired”建议采用固定的(可能是系统定义的)分组。任意集合的更面向用户的情况仍然需要在某些时候进行枚举来实现。

为大小为 N 的组发出 N 个 DELETE 并不具有吸引力,因为复合延迟和缺乏原子性,但 REST DELETE 可能仅针对单个资源。

我认为 Swanliu 的响应所暗示的最佳实践可能是定义一个 POST 操作,该操作能够创建一个资源,该资源成为要删除的对象的新包含父级。 POST 可以返回正文,因此相关系统可以为此非域分组资源创建唯一标识符并将其返回给客户端,客户端可以转身并发出第二个请求来删除它。 POST 创建的资源是短暂的,但却是有目的的——它的消亡级联到作为批量删除操作所需目标的域对象。

> POST /users/bulktarget/create
> uid=3474&uid=8424&uid=2715&uid=1842&uid=90210&uid=227&uid=66&uid=54&uid=8
> ...
< ...
< 200 OK
< ae8f2b00e

> DELETE /users/bulktarget/ae8f2b00e
> ...
< ...
< 200 OK

诚然,两个网络交换比一个网络交换更不可取,但考虑到较小的批量删除是两个对象,并且需要两个 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.

> POST /users/bulktarget/create
> uid=3474&uid=8424&uid=2715&uid=1842&uid=90210&uid=227&uid=66&uid=54&uid=8
> ...
< ...
< 200 OK
< ae8f2b00e

> DELETE /users/bulktarget/ae8f2b00e
> ...
< ...
< 200 OK

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.

难以启齿的温柔 2024-12-12 21:48:19

我对 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?

压抑⊿情绪 2024-12-12 21:48:19

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 special deleteUsers body containing the IDs to delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文