使用添加Mixin注释时,将json读取到Object失败

时间:2016-04-02 06:31:24

标签: json jackson

package com.dh.inheritance;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


public class Client6{

    public static void main(String[] args){

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping(); 
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);         
        objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);         
        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
        objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, true);

        objectMapper.registerModule(new MyModule());

        try {

            String test = null;
            test.length();

        } catch (Exception e) {

            try {
                String jsonString = objectMapper.writeValueAsString(e);
                System.out.println(jsonString);

                **objectMapper.readValue(jsonString, e.getClass());**
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }   

}



package com.dh.inheritance;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;


public abstract class MixIn {

    @JsonIdentityInfo(generator=ObjectIdGenerators.StringIdGenerator.class, property="$id")
    private Throwable cause;

}

package com.dh.inheritance;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class MyModule extends SimpleModule{

    public MyModule() {
        super("test", new Version(2, 7, 2, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(Throwable.class, MixIn.class);      
    }
}
  

{ “显示java.lang.NullPointerException”:{ “detailMessage”:NULL, “原因”:{ “显示java.lang.NullPointerException”:{ “$ ID”: “912da540-bf85-4e5d-869c-acfaebe85bdf”, “detailMessage”:空, “原因”: “912da540-bf85-4e5d-869c-acfaebe85bdf”, “堆栈跟踪”:空}}, “堆栈跟踪”:空}}   com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:   无法识别的字段“$ id”(类java.lang.NullPointerException),而不是   标记为可忽略的(3个已知属性:“cause”,“stackTrace”,   “detailMessage”])在[来源:   { “显示java.lang.NullPointerException”:{ “detailMessage”:空, “原因”:{ “显示java.lang.NullPointerException”:{ “$标识”: “912da540-bf85-4e5d-869c-acfaebe85bdf”, “detailMessage” :NULL, “原因”: “912da540-bf85-4e5d-869c-acfaebe85bdf”, “堆栈跟踪”:空}}, “堆栈跟踪”:空}}; line:1,column:107](通过参考链:   java.lang.NullPointerException [“$ id”])at   com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)   在   com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:855)   在   com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1083)   在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1389)   在   com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:135)   在   com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)   在   com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer._deserialize(AsWrapperTypeDeserializer.java:115)   在   com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject(AsWrapperTypeDeserializer.java:49)   在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1017)   在   com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:488)   在   com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:104)   在   com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)   在   com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer._deserialize(AsWrapperTypeDeserializer.java:115)   在   com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject(AsWrapperTypeDeserializer.java:49)   在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1017)   在   com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)   在   com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)   在   com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2779)   在com.dh.inheritance.Client6.main(Client6.java:38)

抛出异常时,将json转换为Object。

0 个答案:

没有答案