Reputation: 1388
I'm trying to allow to serialize my object excluding some fields. So say I have a class:
class UserInfo {
String userName;
String password;
//getters & setters
}
And I need to serialize it for some purpose but excluding password field. So I added:
@JsonIgnoreProperties({"password"}) //or
@JsonIgnoreProperties(value = {"amountDelta"}, allowGetters = true, allowSetters = true)
Then, I'm trying to wrap an instance of my class via @RequestBody
:
@RequestMapping(value = "test", method = POST, produces = "application/json")
public String testBodyWrapping(@RequestBody UserInfo userInfo) {
return userInfo.getPassword();
}
The password is always null
! (I tested this removing @JsonIgnoreProperties
annotation, and it worked) But creating an instance of a class "by-hand" work as expected and password
field doesn't come out. What do I do wrong?
Upvotes: 0
Views: 1161
Reputation: 1206
When someone use this endpoint, it send json text. Your method deserializing it to UserInfo class object. During this process deserializer trying to set each field of UserInfo class. First it try to set 'userName' field and it's ok, after that it find 'password' field as ignorable and doesn't set in. Thats all.
Upvotes: 1