I'm using Jackson to serialize and deserialize objects. The problem is that sometimes I want to show a field and sometimes not.
Actually I'm using the @JsonIgnore to avoid the printing of the property when I don't need it. When I need it I'm disabling the Property through
mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS);
but this will disable also other annotations that I need.
How can I get the result that I need? Using Views? Any example?
A little pojo to understand what I want:
class User {
private String username;
@JsonIgnore
private String password;
// getter setter
}
writeToDB() {
mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS);
mapper.writeValueAsString(user);
}
and through the REST API you can get the username without the password (thanks to the JsonIgnore)
See Question&Answers more detail:os