Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - How do I access a field from a class that is created using @JsonSubTypes?

I am using Lombok annotations in every class and the code is as follows :

    public class DtoClass {
      private Vehicle metadata;
    }

    @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY. property="type")
    @JsonSubTypes({@JsonSubTypes.Type(value=Car.class, name="car"),{@JsonSubTypes.Type(value=Bike.class, name="bike")}})
    public class Vehicle {
    //this acts as an abstraction class
    }

    @JsonTypeName("car")
    public class Car extends Vehicle{
      @NotNull
      private String carLicensePlate;
      @NotNull
      private String carModel;
    }

    @JsonTypeName("bike")
    public class Bike extends Vehicle{
      @NotNull
      private String bikeLicensePlate;
    }

How can I access the value of the variable carModel in this case? If I use DtoClass.getMetadata() it will return the value of all the variables depending upon if its a Car or Bike, for example:

    { 
    "type" : "car",
    "carLicensePlate" : "ABCDC",
    "carModel" : "Tesla"
    }

but I only need the value of the variable carModel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...