Honestly I do not know of a quite direct way to really let the ObjectMapper
ignore a particular annotation but why not using @JsonView
s?
Just define some views…
public class Views {
public static class Public {
}
public static class Internal {
}
}
…put it on your conditional field…
public class Person {
@JsonView(Views.Internal.class)
public String value;
}
…and serialize it with your mapper…
String result = mapper
.writerWithView(Views.Public.class)
.writeValueAsString(person);
The samples above were taken from good old Baeldung where you can find even more nice variants of this solution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…