1
0

Part 5: polymorphic base type with @JsonTypeInfo and @JsonSubTypes

This commit is contained in:
2026-08-04 17:32:13 +00:00
parent b43a5460ce
commit 198b6b76a8

View File

@@ -0,0 +1,27 @@
package com.ankurm.jackson3.part5polymorphic;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
/**
* Post: Polymorphic Deserialisation — https://ankurm.com/jackson-polymorphic-deserialization/
* Section: "Setting Up the Hierarchy with @JsonTypeInfo and @JsonSubTypes"
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME, // use a logical name as the discriminator
include = JsonTypeInfo.As.PROPERTY, // embed it as a field in the JSON object
property = "paymentType" // the JSON key that carries the type name
)
@JsonSubTypes({
@JsonSubTypes.Type(value = CreditCardPayment.class, name = "credit_card"),
@JsonSubTypes.Type(value = BankTransferPayment.class, name = "bank_transfer")
})
public abstract class PaymentMethod {
private Long paymentId;
private double amountDue;
public Long getPaymentId() { return paymentId; }
public double getAmountDue() { return amountDue; }
public void setPaymentId(Long v) { this.paymentId = v; }
public void setAmountDue(double v) { this.amountDue = v; }
}