我正在努力使用Jackson fasterXml进行反序列化。 我想有类型信息和多态使用的东西。 我在阅读了杰克逊文档和this之后尝试了不同的东西,但仍然无法得到它。序列化顺利进行,但反序列化生成的字符串并不成功。
提前感谢您的帮助
以下是课程:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
private class DynReachSlotInternalRep {
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
private class ListStartStop extends MergeableLinkedList<DynTimeSlot> {
private static final long serialVersionUID = 1L;
}
private ListStartStop[] weekRep = new ListStartStop[7];
@SuppressWarnings("unused")
public ListStartStop[] getWeekRep() {
return weekRep;
}
@SuppressWarnings("unused")
public void setWeekRep(ListStartStop[] weekRep) {
this.weekRep = weekRep;
}
public DynReachSlotInternalRep() {
super();
}
/...
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public class DynTimeSlot extends IntervalComparison<DynTimeSlot> {
private int startHour;
private int startMinute;
private int stopHour;
private int stopMinute;
public int getStartHour() {
return startHour;
}
public void setStartHour(int startHour) {
this.startHour = startHour;
}
public int getStartMinute() {
return startMinute;
}
public void setStartMinute(int startMinute) {
this.startMinute = startMinute;
}
public int getStopHour() {
return stopHour;
}
public void setStopHour(int stopHour) {
this.stopHour = stopHour;
}
public int getStopMinute() {
return stopMinute;
}
public void setStopMinute(int stopMinute) {
this.stopMinute = stopMinute;
}
public DynTimeSlot(int startHour, int startMinute, int stopHour, int stopMinute) {
super();
if ((stopHour < startHour) || ((stopHour == startHour) && (startMinute > stopMinute)))
throw new RuntimeException("Malformed timeslot");
this.startHour = startHour;
this.startMinute = startMinute;
this.stopHour = stopHour;
this.stopMinute = stopMinute;
}
public DynTimeSlot() {
super();
}
/...
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public class MergeableLinkedList<A extends IntervalComparison<A>> extends LinkedList<A> {
/**
*
*/
private static final long serialVersionUID = 1L;
public MergeableLinkedList() {
super();
}
public void insert(A toInsert) {
recursiveInsert(toInsert, this.listIterator());
}
public void delete (A toDelete) {
recursiveDelete(toDelete, this.listIterator());
}
这是堆栈跟踪:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.dynamease.entity.DynReachSlot$DynReachSlotInternalRep]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@277b65b1; line: 1, column: 70]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:157)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:113)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:82)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:894)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:36)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.dynamease.entity.DynReachSlot$DynTimeSlotOperations.getFromDirectoryRepresentation(DynReachSlot.java:145)
at com.dynamease.entity.DynReachSlot.<init>(DynReachSlot.java:53)
at com.dynamease.entity.DynReachSlotTest.test2(DynReachSlotTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)