How to conditionally ignore properties with a Jackson AnnotationIntrospector

后端 未结 1 2020
小鲜肉
小鲜肉 2021-01-13 19:06

I want to create an annotation to make Jackson ignore the annotated fields unless a certain tracing level is set:

public class A {
    @IgnoreLevel(\"Debug\"         


        
相关标签:
1条回答
  • 2021-01-13 19:48

    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.

    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

    0 讨论(0)
提交回复
热议问题