0
votes

When the content type is application/json,@RequestBody can read the request body and deserialized into an Object, but the content type is application/x-www-form-urlencoded, spring-mvc can not support this content type with @RequestBody annotation.

In my project, I encountered this problem, I want to support both of them at the same time, but I don't want to lose the convenience of @RequestBody.

Is there another way to support these two content type and like @RequestBody can auto deserialized request body into an Object?

@RequestMapping(value = "/test")
public String test(@RequestBody User user) {
  return user.toString();
}
1

1 Answers

0
votes

Is there another way to support these two content type and like @RequestBody can auto deserialized request body into an Object?

Negative.

What you can do is use FormHttpMessageConverter to deserialize into a MultiValueMap<String, String>. You can then write an transformer to take that and create a User object.