如何实现`Jackson AnnotationIntrospector`? [英] how to implement `Jackson AnnotationIntrospector`?
问题描述
我想创建两个自定义属性:
I want to create two custom attributes:
class A{
@IgnoreLevel("Debug") String str1;
@IgnoreLevel("Info") String str2;
}
或更喜欢这样:
class A{
@Debug String str1;
@Info String str2;
}
我想要写一个会使objectMapper忽略的配置
And I want the write a configuration that will make objectMapper to ignore
defug
和info
。另一种配置只在序列化和反序列化时忽略带有info的字段。
"defug"
and "info"
. And another configuration to ignore fields with "info" only when serializing and de-serializing.
但是我读过的所有帖子( post1 , post2 )不显示如何实现服装的示例配置
\ AnnotationIntrospector
。
but all the posts i have read (post1, post2) don't show an example how to implement costume configuration
\ AnnotationIntrospector
.
推荐答案
如果你想要子类 JacksonAnnotationIntrospector
,你只需要覆盖 hasIgnoreMarker
,类似于:
If you want to sub-class JacksonAnnotationIntrospector
, you just need to override hasIgnoreMarker
, something like:
@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();
}
但请注意,每个类只发生一次注释内省,因此无法动态更改你使用的标准。
but note that annotation introspection only occurs once per class so you can not dynamically change the criteria you use.
对于更多动态过滤,你可能想要使用JSON过滤器功能,例如参见: http://www.cowtowncoder.com/blog/archives/2011/09/entry_461.html
For more dynamic filtering you may want to rather use JSON Filter functionality, see for example: http://www.cowtowncoder.com/blog/archives/2011/09/entry_461.html
这篇关于如何实现`Jackson AnnotationIntrospector`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!