Empty request body gives 400 error

My Spring controller method looks something like this:

@RequestMapping(method=RequestMethod.PUT, value="/items/{itemname}") public ResponseEntity<?> updateItem(@PathVariable String itemname, @RequestBody byte[] data) { // code that saves item }

This works fine except when a try to put a zero-length item, then I get an HTTP error: 400 Bad Request. In this case my method is never invoked. I was expecting that the method should be invoked with the "data" parameter set to a zero-length array.

Can I make request mapping work even when Content-Length is 0?

I am using Spring framework version 4.1.5.RELEASE.

-------------Problems Reply------------

Setting a new byte[0] will not send any content on the request body. If you set spring MVC logs to TRACE you should see a message saying Required request body content is missing as a root cause of your 400 Bad Request

To support your case you should make your @RequestBody optional

@RequestMapping(method=RequestMethod.PUT, value="/items/{itemname}")
public ResponseEntity<?> updateItem(@PathVariable String itemname, @RequestBody(required = false) byte[] data) {
// code that saves item
}

Category:spring Views:0 Time:2018-05-14

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.103 (s). 11 q(s)