我想创建一个注释以使 Jackson 忽略带注释的字段,除非设置了特定的跟踪级别:
public class A {
@IgnoreLevel("Debug") String str1;
@IgnoreLevel("Info") String str2;
}
或者,如果这更容易实现,我也可以为不同级别提供单独的注释:
public class A {
@Debug String str1;
@Info String str2;
}
取决于配置ObjectMapper
, either
- 序列化和反序列化时应忽略所有“调试”和“信息”字段,或者
- 所有“调试”字段均应被忽略,或
- 所有字段均应序列化/反序列化。
我想这应该可以通过定制来实现AnnotationIntrospector。我有这个post,但它没有显示如何实现自定义的示例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();
}
但请注意,注释内省仅在每个类中发生一次,因此您无法动态更改您使用的条件。
对于更动态的过滤,您可能希望使用 JSON Filter 功能,例如:http://www.cowtowncoder.com/blog/archives/2011/09/entry_461.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)