Disable Jackson mapper for a particular annotation

后端 未结 3 1511
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: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 ret = new ArrayList();
                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);
    

提交回复
热议问题