say we have a POJO in which we can’t add @Annotations to
@Data
public class Pojo {
private String firstName;
private String lastName;
private Integer age;
}
Methods
Programmatically 1
mapper.writeValueAsString(JsonView.with(list) .onClass(Pojo.class, match() .exclude("*") .include("lastName", "age")));
Annotation (Per Class)
we create a MixIn class
@Data abstract class PojoMixIn { @JsonIgnore private String name; }configure ObjectMapper
objectMapper.getSerializationConfig().addMixInAnnotations(Pojo.class, PojoMixIn.class); objectMapper.addMixIn(Pojo.class, PojoMixIn.class); // for Jackson 2.5+
Annotation (All Classes)
If you want to ALWAYS exclude certain properties for any class, you could use setMixInResolver method
@JsonIgnoreProperties({"id", "index", "version"}) abstract class MixIn { } mapper.setMixInResolver(new ClassIntrospector.MixInResolver() { @Override public Class<?> findMixInClassFor(Class<?> cls) { return MixIn.class; } @Override public ClassIntrospector.MixInResolver copy() { return this; } });