如何实现`Jackson AnnotationIntrospector`?
我想创建两个自定义属性:
class A{ @IgnoreLevel("Debug") String str1; @IgnoreLevel("Info") String str2; }
或者更喜欢这样:
class A{ @Debug String str1; @Info String str2; }
我希望写一个配置,使objectMapper被忽略
"defug"
和"info"
。 另一种配置只在序列化和反序列化时忽略带“info”的字段。
但我读过的所有post( post1 , post2 )都没有显示如何实现服装configuration
\ AnnotationIntrospector
的示例。
如果你想要子类JacksonAnnotationIntrospector
,你只需要覆盖hasIgnoreMarker
,例如:
@Override public boolean hasIgnoreMarker(AnnotatedMember m) { IgnoreLevel lvl = m.findAnnotation(IgnoreLevel.class); // use whatever logic necessary if (level.value().equals("Debug")) return true; return super.hasIgnoreMarker(); }
但请注意,注释内省仅在每个类中出现一次,因此您无法动态更改所使用的条件。
要获得更多动态过滤,您可能需要使用JSONfilterfunction,例如,请参阅: http : //www.cowtowncoder.com/blog/archives/2011/09/entry_461.html