SpringBoot: Request body with inheritance

I have following classes

public class Car {
     private String make; 
     private String model; 
     private TransmissionType transmissionType; // Manual/Automatic
     private Transmission transmission; 

public class Transmission {

public class AutomaticTransmission {
     public Technology technology; // DCT/CVT/AMT

public class ManualTransmission {
     public int numGears; 

In this example, when the payload passed could be

    "make": "Toyota", 
    "model": "Iris", 
    "transmissionType": "Automatic", 
    "transmission" {
         "technology": "DCT"

or it could be

    "make": "Merc", 
    "model": "C", 
    "transmissionType": "Manual", 
    "transmission" {
         "numGears": 5

When this is passed to controller as body, it should be able to create respective class. How do I achieve this.

I tried using JsonTypeInfo and JsonSubTypes as shown in this answer but it necessitates me to move transmissionType within Transmission class, which is not the case for me.

Read more here: https://stackoverflow.com/questions/65715242/springboot-request-body-with-inheritance

Content Attribution

This content was originally published by Ganesh Satpute at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: