Reputation: 2039
Jackson annotation works in serializing an object but, it is not work to resolve a request parameters.
There is a class with a Jackson annotation as follow:
public class Role{
@JsonProperty(
value = "description",
defaultValue = "description",
required = false,
access = Access.READ_WRITE)
private String description;
@JsonProperty(
value = "code_name",
defaultValue = "permission",
required = true,
access = Access.READ_WRITE)
private String codeName;
...
}
As you can see property codeName is serialized as code_name in json or xml. For example getting a role is:
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Role getRole(@PathVariable id){
...
}
where the result is:
{
"description": "..",
"code_name": ".."
}
and this is my mvc config:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Now, suppose there is a request map as follow:
@RequestMapping(value = "new", method = RequestMethod.POST)
public Role create(Role role) {
...
}
I fill a form with fields named description and code_name and send to the server. But, description is just set into the input role and the codeName is null.
By the way, it is ok if the form field name replace with codeName.
Upvotes: 0
Views: 137
Reputation: 3522
you need to use
@RequestMapping(value = "new", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public Role create(@RequestBody Role role) {
Upvotes: 1