Ela*_*nda 5 java json annotations jackson annotation-introspector
我想创建一个注释,让 Jackson 忽略带注释的字段,除非设置了某个跟踪级别:
public class A {
@IgnoreLevel("Debug") String str1;
@IgnoreLevel("Info") String str2;
}
Run Code Online (Sandbox Code Playgroud)
或者,如果这更容易实现,我也可以为不同级别分别添加注释:
public class A {
@Debug String str1;
@Info String str2;
}
Run Code Online (Sandbox Code Playgroud)
根据 的配置ObjectMapper,要么
我想这应该可以通过自定义AnnotationIntrospector. 我有这篇文章,但它没有显示如何实现自定义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();
}
Run Code Online (Sandbox Code Playgroud)
但请注意,注释内省每个类只发生一次,因此您不能动态更改您使用的标准。
对于更多动态过滤,您可能希望使用 JSON 过滤器功能,例如参见:http : //www.cowtowncoder.com/blog/archives/2011/09/entry_461.html
| 归档时间: |
|
| 查看次数: |
4179 次 |
| 最近记录: |