当我想反序列化对象“objectabstract1”时,我遇到了一个问题。我有一个错误:
我尝试了几种解决办法,但没有成功。。。如果有人有主意?问题出在objectabstract2上,但我不知道要添加什么
-- serializing --
{"object1Type":"INSTANCE_OBJECT1","autor":"John","objectAbstract2":{"nameProc":"Processus transfert","identifiant":null,"nomObject3":"Demande de transfert"},"state":"En cours","priority":null}
-- deserializing --
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of fr.test.jakson.ObjectAbstract2, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: {"object1Type":"INSTANCE_OBJECT1","autor":"John","objectAbstract2":{"nameProc":"Processus transfert","identifiant":null,"nomObject3":"Demande de transfert"},"state":"En cours","priority":null}; line: 1, column: 49] (through reference chain: fr.test.jakson.Object1["objectAbstract2"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:892)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:122)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:93)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:131)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
at fr.test.jakson.MainTest.main(MainTest.java:33)
对象摘要1:
package fr.test.jakson;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "object1Type")
@JsonSubTypes({ @Type(value = Object1.class, name = "INSTANCE_OBJECT1") })
public abstract class ObjectAbstract1 implements Serializable {
private static final long serialVersionUID = 1L;
private String autor;
private ObjectAbstract2 objectAbstract2;
public ObjectAbstract1() {
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
public ObjectAbstract2 getObjectAbstract2() {
return objectAbstract2;
}
public void setObjectAbstract2(ObjectAbstract2 objectAbstract2) {
this.objectAbstract2 = objectAbstract2;
}
}
目标1:
package fr.test.jakson;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("INSTANCE_OBJECT1")
public class Object1 extends ObjectAbstract1 implements Serializable {
private static final long serialVersionUID = 1L;
private String state;
private String priority;
public Object1() {
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
}
对象摘要2:
package fr.test.jakson;
import java.io.Serializable;
public abstract class ObjectAbstract2 implements Serializable {
private static final long serialVersionUID = 1L;
private String nameProc;
public ObjectAbstract2() {
}
public String getNameProc() {
return nameProc;
}
public void setNameProc(String nameProc) {
this.nameProc = nameProc;
}
}
对象摘要3:
package fr.test.jakson;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectAbstract3Type")
@JsonSubTypes({ @Type(value = Object3.class, name = "Object3"), @Type(value = Object2.class, name = "Object2") })
public abstract class ObjectAbstract3 extends ObjectAbstract2 implements Serializable {
private static final long serialVersionUID = 1L;
private String identifiant;
public ObjectAbstract3() {
}
public String getIdentifiant() {
return identifiant;
}
public void setIdentifiant(String identifiant) {
this.identifiant = identifiant;
}
}
目标2:
package fr.test.jakson;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("Object2")
public class Object2 extends ObjectAbstract3 implements Serializable {
private static final long serialVersionUID = 1L;
private String nomObject2;
public Object2() {
}
public String getNomObject2() {
return nomObject2;
}
public void setNomObject2(String nomObject2) {
this.nomObject2 = nomObject2;
}
}
目标3:
package fr.test.jakson;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("Object3")
public class Object3 extends ObjectAbstract3 implements Serializable {
private static final long serialVersionUID = 1L;
private String nomObject3;
public Object3() {
}
public String getNomObject3() {
return nomObject3;
}
public void setNomObject3(String nomObject3) {
this.nomObject3 = nomObject3;
}
}
主类测试:
package fr.test.jakson;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainTest {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
Object1 object1 = new Object1();
object1.setAutor("John");
object1.setState("En cours");
Object3 object3 = new Object3();
object3.setNomObject3("Demande de transfert");
ObjectAbstract2 objectAbstract2 = (ObjectAbstract2) object3;
objectAbstract2.setNameProc("Processus transfert");
ObjectAbstract1 objectAbstract1 = (ObjectAbstract1) object1;
objectAbstract1.setObjectAbstract2(objectAbstract2);
System.out.println("-- serializing --");
ObjectMapper om = new ObjectMapper();
String s = om.writeValueAsString(objectAbstract1);
System.out.println(s);
System.out.println("-- deserializing --");
ObjectAbstract1 instanceTacheDeserializing;
ObjectMapper om1 = new ObjectMapper();
String jsonTest = "{\"object1Type\":\"INSTANCE_OBJECT1\",\"autor\":\"John\",\"objectAbstract2\":{\"nameProc\":\"Processus transfert\",\"identifiant\":null,\"nomObject3\":\"Demande de transfert\"},\"state\":\"En cours\",\"priority\":null}";
instanceTacheDeserializing = om1.readValue(jsonTest, ObjectAbstract1.class);
}
}
1条答案
按热度按时间8dtrkrch1#
objectabstract2不能是抽象类。反序列化程序需要具体的类来转换为java对象。像下面这样更改objectabstract2类