1

我有一个类层次结构如下

public class Car {
     private String make; 
     private String model; 
     
     @Schema(example = "MANUAL")
     private TransmissionType transmissionType;

     @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, 
          property = "transmissionType")
     private Transmission transmission; 
}

@JsonSubTypes({
        @JsonSubTypes.Type(value = AutomaticTransmission.class, name = "AUTOMATIC"),
        @JsonSubTypes.Type(value = ManualTransmission.class, name = "MANUAL"))
})
public abstract class Transmission {
}

public class AutomaticTransmission {
     @Schema(example = "DCT")
     public Technology technology;
}

public class ManualTransmission {
     @Schema(example = "5")
     public int numGears; 
}

现在,当 swagger 生成时,我看到汽车模型,

{
   "transmissionType": "MANUAL"
   "transmission": {  
       "technology": "DCT"
   }
}

这里的传输类型是手动的,但给出了自动的例子。要求以手动变速器为例。我如何链接这两个属性。

我知道我可以创建一个示例 json 并将其放入,@Schema(example = "{\"numGears\": 5}")但这会在修改类时产生修改 json 的维护开销。

4

0 回答 0