
[英]What are @JsonTypeInfo and @JsonSubTypes used for in jackson
[英]How to get the Dynamic Json payload if it is serialized using @JsonTypeInfo and @JsonSubTypes
我正在为公共 class PaymentMethodRefOrValue 使用 @JsonTypeInfo 和 @JsonSubTypes
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = CashType.class, name = "CashType"),
@JsonSubTypes.Type(value = CheckType.class, name = "CheckType")
})
这些是付款方式,但是 PaymentMethodRefOrValue 中不存在某些字段。
如果我想访问付款方式 id(PaymentMethodRefOrValue 类的一部分),我可以做refundCreate.getPaymentMethod().getId()
但我想使用 CheckType 和 CashType 类的方法
示例 Json Payload 支付方法
"paymentMethod":{
"checkId":"112233445566",
"bank":"123456",
"@type": "CheckType"
}
我想访问 checkId 和银行字段。 我知道以下解决方案
String json = "static payload"
CheckType checktype = new ObjectMapper().readerFor(PaymentMethodRefOrValue.class).readValue(json);
String checkId = checktype.getCheckId();
CheckType class 的快照
@JsonTypeName("CheckType")
public class CheckType extends PaymentMethodRefOrValue {
@JsonProperty("checkId")
private String checkId = null;
@JsonProperty("bank")
private String bank = null;
...
我不能使用 readValue() 方法,因为我正在使用 postman 来传递有效负载。 有人可以帮帮我吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.