I'm receiving error 400 when I send PATCH request to my endpoint that looks like this
@RequestMapping(value = "...",
method = RequestMethod.PATCH,
consumes = "application/json",
produces = "application/json")
@ResponseBody
public User updateUserPartial(@PathVariable("userId") String userId,
@RequestBody Map<String, Object> userMap,
@RequestBody User user,
HttpServletResponse response) {
...
}
so basically both userMap and user should contain same data in different structure. If I omit one @RequestBody value, this seems to work correctly. Is it somehow possible to have both @RequestBody values?
There can be only one body parameter, although the operation may have other parameters (path, query, header). Note: The payload of the application/x-www-form-urlencoded and multipart/form-data requests is described by using form parameters, not body parameters.
Can we use RequestBody and RequestParam together? nothing. So it fails with 400 because the request can't be correctly handled by the handler method. The handler for @RequestParam acts first, reading what it can of the HttpServletRequest InputStream to map the request parameter, ie.
@ModelAttribute is used for binding data from request param (in key value pairs), but @RequestBody is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.
You cannot use two @RequestBody as it can bind to a single object only (the body can be consumed only once). As Luke explained the easiest would be to create one object that will capture all the relevent data, and than create the objects you have in the arguments.
On the other hand, if you're insisting on your approach, you can create a custom ArgumentResolver as explained here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With