Reputation: 5032
is there a way to tell to Jackson to ignore fields during serialization which are annoted with non jackson annotation ?
for example :
@SomeAnnotation
private String foo;
I know there is jackson annotation to do that, but my fields are already annotated with my persistence annotation, so I'd like to avoid duplication since I already have field with annotation I'd like to ignore
Upvotes: 3
Views: 1205
Reputation: 3959
I would encourage you to just use @JsonIgnore
because otherwise you're hiding something that's going on for those particular methods and dual-purposing annotations.
However... you can accomplish this by extending JacksonAnnotationIntrospector
and overriding _isIgnorable(Annotated)
like this:
publi class MyAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
protected boolean _isIgnorable(Annotated a) {
boolean isIgnorable = super._isIgnorable(a);
if (!isIgnorable) {
SomeAnnotation ann = a.getAnnotation(SomeAnnotation.class);
isIgnorable = ann != null;
}
return isIgnorable;
}
}
Then set the annotation introspector on your object mapper:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new MyAnnotationIntrospector());
Upvotes: 6