7

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?

2 Answers 2

7

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

3

I'm pretty sure that won't work. There may be a workaround, but the much easier way would be to introduce a wrapper Object and change your signature.

Here you will find more info about it: Spring MVC controller with multiple @RequestBody

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.