禁用注释时,jackson序列化程序抛出NullPointerException

时间:2014-09-02 05:35:40

标签: java annotations jackson serializer

 objectMapper = new ObjectMapper();
 objectMapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS, false);

我使用上面的代码来禁用注释,但是当运行到

 protected void _addMixUnders(Method src, AnnotatedMethod target)
{
    for (Annotation a : src.getDeclaredAnnotations()) {
        if (_annotationIntrospector.isHandled(a)) {
            target.addIfNotPresent(a);
        }
    }
}

在AnnotatedClass中,_ annotationIntrospector为null,因此抛出NullPointerException。 我该怎么办 ?感谢。

java.lang.NullPointerException
at org.codehaus.jackson.map.introspect.AnnotatedClass._addMixUnders(AnnotatedClass.java:974)
at org.codehaus.jackson.map.introspect.AnnotatedClass._addMemberMethods(AnnotatedClass.java:637)
at org.codehaus.jackson.map.introspect.AnnotatedClass.resolveMemberMethods(AnnotatedClass.java:413)
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.classWithCreators(BasicClassIntrospector.java:185)
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:157)
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:96)
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:16)
at org.codehaus.jackson.map.SerializationConfig.introspect(SerializationConfig.java:973)
at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:251)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:758)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:380)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.resolve(BeanSerializerBase.java:307)
at org.codehaus.jackson.map.ser.impl.SerializerCache.addAndResolveNonTypedSerializer(SerializerCache.java:159)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:744)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:344)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findTypedValueSerializer(StdSerializerProvider.java:420)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:601)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2575)
at org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2097)

1 个答案:

答案 0 :(得分:1)

我知道这已经晚了3年,但对于后代来说,这对我有用:

// Don't use this:
// objectMapper.disable(SerializationConfig.Feature.USE_ANNOTATIONS);
// Use this instead:
objectMapper.setAnnotationIntrospector(AnnotationIntrospector.nopInstance());

这在1.9.13-atlassian-1中对我有用。