Spring Web MVC:不再有 HTTP DELETE 的请求体
Spring Web MVC: no request body possible for HTTP DELETE anymore
我有一个问题要问 Spring Web MVC 的开发人员。
简而言之:以前可以在 HTTP DELETE 消息中发送请求正文,但现在不可能了。为什么?
详细:
我们正在使用 spring-webmvc-4.2.4.RELEASE
。
@RestController
public class Controller {
@RequestMapping(value = "/{pathVariable}/deleteAnything", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteAnything(@PathVariable String pathVariable,
@Valid @RequestBody Set<Pojo> pojoSet) {
...
我们送
DELETE /anything/deleteAnything HTTP/1.1
Content-Type: application/json
Host: example.com
[ {
"any field" : "Any value"
} ]
并获取异常
m.m.a.RequestResponseBodyMethodProcessor : Read [java.util.Set<packagename.Pojo>] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@333825a3]
.w.s.m.m.a.ServletInvocableHandlerMethod : Error resolving argument [1] [type=java.util.Set]
HandlerMethod details:
Controller [packagename.Controller]
Method [public org.springframework.http.ResponseEntity<?> packagename.Controller.deleteAnything(java.lang.String,java.util.Set<packagename.Pojo>)]
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<?> packagename.Controller.deleteAnything(java.lang.String,java.util.Set<packagename.Pojo>)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:151)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125)
...
似乎请求正文已被删除。
如果我们在任何地方都使用 HTTP POST 而不是 HTTP DELETE,它工作正常。
以前它工作得很好(抱歉我不能指定以前因为我们的依赖关系非常复杂。如果对你有帮助,我可以post一个旧的build.gradle
).
为什么不可能了?
好像是zuul的问题。没有 zuul 它就可以工作。 Spring与此无关
您可能应该重新设计 API,因为 DELETE 请求中的负载 应该 被忽略。
来自https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5:
A payload within a DELETE request message has no defined semantics.
来自https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3:
If the request method does not include defined semantics for an
entity-body, then the message-body SHOULD be ignored when handling the
request.
我有一个问题要问 Spring Web MVC 的开发人员。
简而言之:以前可以在 HTTP DELETE 消息中发送请求正文,但现在不可能了。为什么?
详细:
我们正在使用 spring-webmvc-4.2.4.RELEASE
。
@RestController
public class Controller {
@RequestMapping(value = "/{pathVariable}/deleteAnything", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteAnything(@PathVariable String pathVariable,
@Valid @RequestBody Set<Pojo> pojoSet) {
...
我们送
DELETE /anything/deleteAnything HTTP/1.1
Content-Type: application/json
Host: example.com
[ {
"any field" : "Any value"
} ]
并获取异常
m.m.a.RequestResponseBodyMethodProcessor : Read [java.util.Set<packagename.Pojo>] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@333825a3]
.w.s.m.m.a.ServletInvocableHandlerMethod : Error resolving argument [1] [type=java.util.Set]
HandlerMethod details:
Controller [packagename.Controller]
Method [public org.springframework.http.ResponseEntity<?> packagename.Controller.deleteAnything(java.lang.String,java.util.Set<packagename.Pojo>)]
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<?> packagename.Controller.deleteAnything(java.lang.String,java.util.Set<packagename.Pojo>)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:151)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125)
...
似乎请求正文已被删除。
如果我们在任何地方都使用 HTTP POST 而不是 HTTP DELETE,它工作正常。
以前它工作得很好(抱歉我不能指定以前因为我们的依赖关系非常复杂。如果对你有帮助,我可以post一个旧的build.gradle
).
为什么不可能了?
好像是zuul的问题。没有 zuul 它就可以工作。 Spring与此无关
您可能应该重新设计 API,因为 DELETE 请求中的负载 应该 被忽略。
来自https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5:
A payload within a DELETE request message has no defined semantics.
来自https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3:
If the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.