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);
try {
String test = null;
test.length();
} catch (Exception e) {
try {
String jsonString = objectMapper.writeValueAsString(e);
System.out.println(jsonString);
} catch (JsonProcessingException e1) {
e1.printStackTrace();
}
}
}
}
当我执行上面的代码时,我得到的是异常。让我知道,如何解决这个问题。
com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: java.lang.NullPointerException["cause"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:230)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:879)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:666)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:569)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980)
at com.dh.inheritance.Client6.main(Client6.java:33)
答案 0 :(得分:1)
它正在代码下面写成json string。
objectMapper.registerModule(new MyModule());
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);
}
}
但是将json读取到Object失败了。 输出:
{ “显示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:45)