gpt4 book ai didi

java - Jackson 异常的序列化和反序列化

转载 作者:搜寻专家 更新时间:2023-11-01 03:54:06 24 4
gpt4 key购买 nike

JSON nest class data binding正如答案所做的那样,当我对容器进行序列化和反序列化时,我得到了这个异常:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not find a Value deserializer for abstract type [simple type, class com.xiaonei.wap.ps.model.Container]
at org.codehaus.jackson.map.deser.StdDeserializerProvider._handleUnknownValueDeserializer(StdDeserializerProvider.java:321)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:116)
at org.codehaus.jackson.map.deser.StdDeserializer.findDeserializer(StdDeserializer.java:260)
at

最佳答案

简单地删除或重命名 type 字段的 setter/getter

public class Container 
{
private AAnimal animal;

public AAnimal getAnimal()
{
return animal;
}

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="type")
@JsonSubTypes({ @Type(value = Cat.class, name = "cat"), @Type(value = Dog.class, name = "dog") })
public void setAnimal(AAnimal animal)
{
this.animal = animal;
}
}

public abstract class AAnimal
{
public String name;
}

测试

  String json = "{\"type\":\"cat\", \"animal\" : {\"name\":\"cat-mew\" }}";
Container val = o.readValue(json, Container.class);
System.out.println(o.writeValueAsString(val));

输出是

{"animal":{"name":"cat-mew"},"type":"cat"}

关于java - Jackson 异常的序列化和反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com