RequestMethod.DELETE 不接受 RequestBody

RequestBody not accepted for RequestMethod.DELETE

我正在使用 tomcat v8 并尝试通过 RequestBody post 一个对象到我的 REST API。此 REST API 基本上是 RequestMethod.DELETE,因为此 API 中的逻辑是删除在 RequestBody 中传递的对象。

这对我不起作用。我不得不稍后将我的方法转换为 POST 但我仍然想知道 DELETE 不接受 RequestBody 吗?

这是 Spring 框架或 REST 原则的限制还是与我的 Tomcat 配置 (server.xml) 文件有关。

Is this the restriction from the Spring framework or REST principles or something to do with my Tomcat configuration (server.xml) file

是的,您的 Tomcat 配置有问题。

您必须在 server.xml 中启用解析 DELETE 方法(同样适用于 PUT),如下所示:

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               parseBodyMethods="POST,PUT,DELETE"
               redirectPort="8443" />

but I am still wondering does DELETE doesn't accept RequestBody ?

这是预期的行为。 RFC 7231 版本的 HTTP 1.1 规范指出:

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

在这种情况下,您似乎已将 运行 纳入 Tomcat 中的默认限制。 (根据 HTTP 规范,这是完全合法的行为。)

您可以覆盖默认行为:请参阅 。但是,让我感到震惊的是 糟糕的 API 设计 DELETE 动词允许或要求请求正文。这当然是违反直觉的。