gpt4 book ai didi

java - jackson 多态反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:12 26 4
gpt4 key购买 nike

Jackson 和类型层次结构存在以下问题。我正在序列化一个 SubA 类,它将 Base 扩展为 String,然后尝试将其反序列化。当然在编译的时候,系统不知道会不会BaseSubA 所以我期待一个 Base 并且之后会做一些其他操作,如果它是一个 SubA.

我的 Base 类如下所示:

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = SubA.class, name = "SubA")
})
public class Base {
protected String command; // +get +set
protected String type; // +get +set
}

...和一个派生自 Base 的类:

@JsonTypeName("SubA")
public class SubA extends Base {
private AnotherClass anotherClass; // +get +set
private String test; // +get +set
@JsonIgnore
@Override
public String getType() {
return "SubA";
}
}

...我正在尝试执行以下代码:

ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
Base payload = new SubA(); // + setting anotherClass as well as test variables
String requestStringSend = ow.writeValueAsString(payload);
System.out.println("Sending: " + requestStringSend);
Base received = mapper.readValue(requestStringSend, Base.class);
String requestStringReceived = ow.writeValueAsString(received);
System.out.println("Received: " + requestStringReceived);

字符串 requestStringSend 是:

Sending: {
"command" : "myCommand",
"type" : "SubA",
"anotherClass" : {
"data" : "someData"
},
"test" : "test123"
}

但是我不断地重复同样的错误。映射器现在知道如何处理 anotherClass 参数 - 它不存在于 Base 中。但我认为映射器会将其转换为 SubA 类?

Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "anotherClass" (Class com.test.Base), not marked as ignorable
at [Source: java.io.StringReader@1256ea2; line: 4, column: 21] (through reference chain: com.test.Base["anotherClass"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:649)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:635)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1355)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:717)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.test.Foo.main(Foo.java:32)

我查看了以下问题/资源:

最佳答案

您的代码对于用例来说看起来是正确的。一个可能的问题是,您可能不小心将 Jackson 2 注释与 Jackson 1 ObjectMapper 一起使用(我可以从异常包名称中看到后者是 Jackson)。注释和映射器的版本必须匹配;否则注释将被忽略,这将解释您所看到的问题。

关于java - jackson 多态反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029707/

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