Disable Jackson mapper for a particular annotation

后端 未结 3 1512
Happy的楠姐
Happy的楠姐 2020-12-30 14:37

With Jackson, it\'s easy to disable all annotations for a given ObjectMapper. Is there a way to only disable one given annotation?

// disable al         


        
相关标签:
3条回答
  • 2020-12-30 15:09

    This the best I've come across. I think I saw it on the Jackson user group forums somewhere.

    Essentially it makes a custom annotation introspector, which returns null if it sees that it has a specific annotation (in this case JsonTypeInfo)

    JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() {
                @Override
                protected TypeResolverBuilder<?> _findTypeResolver(
                        MapperConfig<?> config, Annotated ann, JavaType baseType) {
                    if (!ann.hasAnnotation(JsonTypeInfo.class)) {
                        return super._findTypeResolver(config, ann, baseType);
                    }
                    return null;
                }
            };
    
            mapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector);
    
    0 讨论(0)
  • 2020-12-30 15:10

    This solution worked for me. Check this for more info

     private static final JacksonAnnotationIntrospector IGNORE_ENUM_ALIAS_ANNOTATIONS = new JacksonAnnotationIntrospector() {
    
        @Override
        protected <A extends Annotation> A _findAnnotation(final Annotated annotated, final Class<A> annoClass) {
            if (!annotated.hasAnnotation(JsonEnumAliasSerializer.class)) {
                return super._findAnnotation(annotated, annoClass);
            }
            return null;
        }
    };
    

    And my custom annotation:

    @Retention(RetentionPolicy.RUNTIME)
    @JacksonAnnotationsInside
    @JsonSerialize(using = JsonEnumSerializer.class)
    public @interface JsonEnumAliasSerializer {
    }
    

    And ObjectMapper:

        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setAnnotationIntrospector(IGNORE_ENUM_ALIAS_ANNOTATIONS);
    
    0 讨论(0)
  • 2020-12-30 15:25

    I think it's a better idea to override findPropertiesToIgnore method like this:

        JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() {
            @Override
            public String[] findPropertiesToIgnore(AnnotatedClass ac) {
                ArrayList<String> ret = new ArrayList<String>();
                for (Method m : ac.getRawType().getMethods()) {
                    if(ReflectionUtils.isGetter(m)){
                        if(m.getAnnotation(Transient.class) != null)
                            ret.add(ReflectionUtils.getPropertyName(m));
    
                    }
                };
                return ret.toArray(new String[]{});
            }
        };
        objectMapper = new ObjectMapper();
        objectMapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector);
    
    0 讨论(0)
提交回复
热议问题