从包装器中的类型反序列化 Jackson 中的动态实体 class
Deserialize dynamic entity in Jackson from type in wrapper class
我有以下 class 用于解析 API 请求:
public class SkydropXApiResponse {
private String id;
private String type;
private SkydropXEntity attributes;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public SkydropXEntity getAttributes() { return attributes; }
public void setAttributes(SkydropXEntity attributes) { this.attributes = attributes; }
}
SkydropXEntity.java
是由具体类型扩展的 class,具有以下内容:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "parcels", value = SkydropXParcel.class),
@JsonSubTypes.Type(name = "rates", value = SkydropXQuotation.class),
@JsonSubTypes.Type(name = "addresses", value = SkydropXAddress.class),
@JsonSubTypes.Type(name = "labels", value = SkydropXLabel.class),
@JsonSubTypes.Type(name = "shipments", value = SkydropXShipment.class),
})
public abstract class SkydropXEntity {}
我知道我可以使用类型 属性 让 Jackson 知道将什么转换成具体类型,方法是将其作为 属性,但它必须在父 [=23] 中=] 为了这个工作。
有什么方法可以使用 SkydropXApiResponse
中的 type
属性 来告诉 Jackson 它应该在哪些具体类型中反序列化 POJO?
对于偶然发现此问题的任何人,我已经找到了答案!
我没有将 @JsonTypeInfo
注释添加到我的摘要 class,而是最终将它们添加到我的包装 class.
上的 attributes
属性
public class SkydropXApiResponse {
private String id;
private String type;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
@JsonSubTypes(value = {
@JsonSubTypes.Type(name = "parcels", value = SkydropXParcel.class),
@JsonSubTypes.Type(name = "rates", value = SkydropXQuotation.class),
@JsonSubTypes.Type(name = "addresses", value = SkydropXAddress.class),
@JsonSubTypes.Type(name = "labels", value = SkydropXLabel.class),
@JsonSubTypes.Type(name = "shipments", value = SkydropXShipment.class),
})
private SkydropXEntity attributes;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public SkydropXEntity getAttributes() { return attributes; }
public void setAttributes(SkydropXEntity attributes) { this.attributes = attributes; }
}
我有以下 class 用于解析 API 请求:
public class SkydropXApiResponse {
private String id;
private String type;
private SkydropXEntity attributes;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public SkydropXEntity getAttributes() { return attributes; }
public void setAttributes(SkydropXEntity attributes) { this.attributes = attributes; }
}
SkydropXEntity.java
是由具体类型扩展的 class,具有以下内容:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "parcels", value = SkydropXParcel.class),
@JsonSubTypes.Type(name = "rates", value = SkydropXQuotation.class),
@JsonSubTypes.Type(name = "addresses", value = SkydropXAddress.class),
@JsonSubTypes.Type(name = "labels", value = SkydropXLabel.class),
@JsonSubTypes.Type(name = "shipments", value = SkydropXShipment.class),
})
public abstract class SkydropXEntity {}
我知道我可以使用类型 属性 让 Jackson 知道将什么转换成具体类型,方法是将其作为 属性,但它必须在父 [=23] 中=] 为了这个工作。
有什么方法可以使用 SkydropXApiResponse
中的 type
属性 来告诉 Jackson 它应该在哪些具体类型中反序列化 POJO?
对于偶然发现此问题的任何人,我已经找到了答案!
我没有将 @JsonTypeInfo
注释添加到我的摘要 class,而是最终将它们添加到我的包装 class.
attributes
属性
public class SkydropXApiResponse {
private String id;
private String type;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
@JsonSubTypes(value = {
@JsonSubTypes.Type(name = "parcels", value = SkydropXParcel.class),
@JsonSubTypes.Type(name = "rates", value = SkydropXQuotation.class),
@JsonSubTypes.Type(name = "addresses", value = SkydropXAddress.class),
@JsonSubTypes.Type(name = "labels", value = SkydropXLabel.class),
@JsonSubTypes.Type(name = "shipments", value = SkydropXShipment.class),
})
private SkydropXEntity attributes;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public SkydropXEntity getAttributes() { return attributes; }
public void setAttributes(SkydropXEntity attributes) { this.attributes = attributes; }
}