Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Web MVC: no request body possible for HTTP DELETE anymore

I have a question for the developers of Spring Web MVC.

In a nutshell: previously it was possible to send a request body in an HTTP DELETE message, but now it is not possible anymore. Why?

In detail:

We are using 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) {
        ...

We send

DELETE /anything/deleteAnything HTTP/1.1
Content-Type: application/json
Host: example.com

[ {
  "any field" : "Any value"
} ]

and get the exception

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)
    ...

It seems that the request body has been removed.

If we use HTTP POST instead of HTTP DELETE everywhere, it works fine.

Previously it worked fine (sorry that I cannot specify previously because our dependencies are very complicated. If it helps you, I can post an old build.gradle).

Why is it not possible anymore?

like image 589
Johannes Flügel Avatar asked Feb 25 '16 07:02

Johannes Flügel


People also ask

Can http delete have request body?

Yes it is allowed to include a body on DELETE requests, but it's semantically meaningless. What this really means is that issuing a DELETE request with a request body is semantically equivalent to not including a request body.

Is @RequestBody required with @RestController?

Remember, we don't need to annotate the @RestController-annotated controllers with the @ResponseBody annotation since Spring does it by default.

Should delete response have a body?

HTTP DELETE Response CodesA successful response MUST be 200 (OK) if the server response includes a message body, 202 (Accepted) if the DELETE action has not yet been performed, or 204 (No content) if the DELETE action has been completed but the response does not have a message body.


2 Answers

You probably should redesign your API, as payloads within DELETE requests should be ignored.

From https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5:

A payload within a DELETE request message has no defined semantics.

From 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.

like image 95
sp00m Avatar answered Oct 19 '22 02:10

sp00m


It seems to be a problem with zuul. Without zuul it works. Spring has nothing to do with it.

like image 22
Johannes Flügel Avatar answered Oct 19 '22 02:10

Johannes Flügel