Spring MVC how @RequestBody works
java, spring, spring-mvc
Solution
No, it's a job for `@ModelAttribute`, not `@RequestBody`.
`@ModelAttribute` populates fields of the target object with values of the corresponding request parameters, performing conversions if necessary. It can be used for requests generated by HTML forms, links with parameters, etc.
`@RequestBody` converts requests to object using one of preconfigured `HttpMessageConverter`s. It can be used for requests containing JSON, XML, etc. However, there is no `HttpMessageConverter` that replicates behavior of `@ModelAttribute`.
Problem
I thought that `@RequestBody` tries to map request `params` to the object after the annotation by the property names. But if I got: ``` @RequestMapping(value = "/form", method = RequestMethod.GET) public @ResponseBody Person formGet(@RequestBody Person p,ModelMap model) { return p; } ``` The request: ``` http://localhost:8080/proj/home/form?id=2&name=asd ``` Return 415 When I change `@RequestBody Person p` with `@RequestParam Map<String, String> params` it's OK: ``` @RequestMapping(value = "/form", method = RequestMethod.GET) public @ResponseBody Person formGet(@RequestParam Map<String, String> params) { return new Person(); } ``` Person class: ``` public class Person{ private long id; private String name; public Person() { } public Person(long id, String name) { super(); this.id = id; this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` Spring vresion `3.2.3.RELEASE` Where did I go wrong?