[英]Polymorphic deserialization of JSON with jackson with type info from parent node
[英]Unable to process JSON from Jersey via Jackson on Deserialization of polymorphic type
我使用JsonTypeInfo將json正確反序列化為適當的子類型
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public abstract class Car {
}
public class Audi extends Car {
// Getters and Setters
}
public class Merc extends Car {
// Getters and Setters
}
對於以下結構Map<String, Car> info
這是我的示例JSON
{
info: {
"xyz": {
type: "Audi"
},
"abc": {
type: "Merc"
}
}
}
上面的JSON無法正確反序列化,並顯示錯誤400:無法處理JSON。 我在這里想念什么?
兩件事,JSON是不正確的,因為屬性需要在引號之間:
{
"info": {
"xyz": {
"type": "Audi"
},
"abc": {
"type": "Merc"
}
}
}
第二,嘗試將此注釋添加到抽象類中:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Audi.class, name = "Audi"),
@JsonSubTypes.Type(value = Merc.class, name = "Merc")})
abstract class Car {
它基本上指示子類型和關聯的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.