6

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 Answers2

6

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

Community
  • 1
  • 1
Master Slave
  • 24,735
  • 4
  • 53
  • 54
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

Community
  • 1
  • 1
Luke SpringWalker
  • 1,550
  • 2
  • 14
  • 32