I need to add @JsonIgnore annotated fields while serializing an object by Jackson ObjectMapper. I know you may offer me to remove the @JsonIgnore
public class MainProgram {
@JsonFilter("nameRemoveFilter")
public static class User{
private String name;
private String age;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider().addFilter("nameRemoveFilter",
SimpleBeanPropertyFilter.filterOutAllExcept("name","age"));
// and then serialize using that filter provider:
User user = new User();
try {
String json = mapper.writer(filters).writeValueAsString(user);
System.out.println(json);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Works for Latest version of Jackson after 2.0