Spring boot 如何消除Spring Rest控制器中代码优先招摇的重复

Spring boot 如何消除Spring Rest控制器中代码优先招摇的重复,spring-boot,swagger,Spring Boot,Swagger,我想先编写代码(而不是先设计),但我面临的代码并不枯燥。下面说明了我的意思,我希望Swigger UI适当显示两个端点:一个是必需的amount字段,另一个是可选的amount字段(这是一个简化的用例) 资源1: @PostMapping(value = ["/createMoneyAmount"], produces = ["application/json"], consumes = ["application/json"]) fu

我想先编写代码(而不是先设计),但我面临的代码并不枯燥。下面说明了我的意思,我希望Swigger UI适当显示两个端点:一个是必需的
amount
字段,另一个是可选的
amount
字段(这是一个简化的用例)

资源1:

@PostMapping(value = ["/createMoneyAmount"], produces = ["application/json"], consumes = ["application/json"])
fun createMoneyAmount(
        @RequestBody @Valid amount: MoneyAmount): Confirmation {
        return create(amount)
}
资源2:

@PostMapping(value = ["/createAltMoneyAmount"], produces = ["application/json"], consumes = ["application/json"])
fun createAltMoneyAmount(
        @RequestBody @Valid amount: AltMoneyAmount): Confirmation {
        return createAlt(amount)
}
其中它们之间的唯一区别是请求主体类型

但是,我想使用相同的类型,因为它们在定义上是相同的,除了通过单个Swagger属性注释为所需的单个属性:
required=true

使用
required=true
属性初始化:

@ApiModel(description = "An amount of money")
@JsonIgnoreProperties(ignoreUnknown = true)
data class MoneyAmount (

        @JsonSerialize(using = Padding::class)
        @ApiModelProperty(name = "amount", value = "The amount of money",
                required = true)
        var amount: BigDecimal? = null,
        @ApiModelProperty(value = "The currency")
        var currency: String? = null
)
@ApiModel(description = "An amount of money")
@JsonIgnoreProperties(ignoreUnknown = true)
data class AltMoneyAmount (

        @JsonSerialize(using = Padding::class)
        @ApiModelProperty(name = "amount", value = "The amount of money")
        var amount: BigDecimal? = null,
        @ApiModelProperty(value = "The currency")
        var currency: String? = null
)
Class不带
required=true
属性:

@ApiModel(description = "An amount of money")
@JsonIgnoreProperties(ignoreUnknown = true)
data class MoneyAmount (

        @JsonSerialize(using = Padding::class)
        @ApiModelProperty(name = "amount", value = "The amount of money",
                required = true)
        var amount: BigDecimal? = null,
        @ApiModelProperty(value = "The currency")
        var currency: String? = null
)
@ApiModel(description = "An amount of money")
@JsonIgnoreProperties(ignoreUnknown = true)
data class AltMoneyAmount (

        @JsonSerialize(using = Padding::class)
        @ApiModelProperty(name = "amount", value = "The amount of money")
        var amount: BigDecimal? = null,
        @ApiModelProperty(value = "The currency")
        var currency: String? = null
)
是否有任何方法(优雅或其他方式——最好是先编写代码)来避免创建两个基本相同的类,以便Swagger发出正确的文档——在本例中,指定
amount
字段在一个请求路径的主体中是“必需的”(通过红点),在另一个请求路径的主体中是可选的