摇摇欲坠的多态无法正常工作

时间:2019-02-04 21:41:47

标签: swagger swagger-ui springfox

我正在使用springfox版本2.9.2和大头贴注释1.5.x。 ApiModel批注支持区分器,subTypes和parent属性,这是使多态性起作用所必需的,但我看不到生成用于启用多态性的正确apidocs。

这是我的注释代码。

@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {
    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleController.class);

    @PostMapping(consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    void post(@RequestBody Vehicle anyVehicle) {
        LOGGER.info("Vehicle : {}", anyVehicle);
    }
}

@ApiModel(discriminator = "type", subTypes = {Car.class, Bike.class})
public class Vehicle {
    String brand;
    String type;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


@ApiModel(parent = Vehicle.class)
public class Car extends Vehicle {
    int noOfDoors;
    boolean powerWindows;

    public int getNoOfDoors() {
        return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
        this.noOfDoors = noOfDoors;
    }

    public boolean isPowerWindows() {
        return powerWindows;
    }

    public void setPowerWindows(boolean powerWindows) {
        this.powerWindows = powerWindows;
    }
}

@ApiModel(parent = Vehicle.class)
public class Bike extends Vehicle {
    boolean pillion;

    public boolean isPillion() {
        return pillion;
    }

    public void setPillion(boolean pillion) {
        this.pillion = pillion;
    }
}

基本上在生成文档时显示一个端点,该端点处理POST请求并以Vehicle为模型。

This is how the generated UI looks

我在这里做的应该工作吗?有人可以指出我可以使用的SpringFox的工作示例吗?

1 个答案:

答案 0 :(得分:1)

Swagger UI中尚不支持discriminator。您可以按照以下问题进行状态更新:

Discriminator does not switch schema
subTypes not displayed in model