Swagger请求自定义类为Number

时间:2016-12-09 12:21:40

标签: jax-rs swagger

我已将BigDecimal类扩展为Premium类,因为我想添加一些默认行为(即MathContext)。在创建的swagger.json中,BigDecimal表示为:

"schema": {
    "type": "number"
}

Premium类显示为:

"schema": {
    "$ref": "#/definitions/Premium"
}

"definitions": {
    "Premium": {
        "type": "object"
    }
}

如何对Premium类进行注释,使其在swagger.json中表示为"type": "number"

Premium类看起来如下:

public class Premium extends BigDecimal {

    private static final long serialVersionUID = 1L;
    private static final MathContext DEFAULT_MC = MathContext.DECIMAL64;

    public Premium(String val) {
        super(val);
    }

    @JsonCreator
    public Premium(@JsonProperty BigDecimal bd) {
        this(bd.toString());
    }

    public Premium multiply(Premium val) {
        return new Premium(super.multiply(val));
    }

    public Premium divide(Premium val) {
        return new Premium(super.divide(val, DEFAULT_MC));
    }

    public Premium subtract(Premium val) {
        return new Premium(super.subtract(val));
    }

}

我创建了一个REST服务,如下所示:

@Path("/test")
@Api
public class PremiumSvc {

    @POST
    @Path("/pr")
    @ApiOperation("Premium.")
    public Premium doPr(Premium req) {
        return req.subtract(new Premium("0.02"));
    }

    @POST
    @Path("/bd")
    @ApiOperation("BigDecimal.")
    public BigDecimal doBd(BigDecimal req) {
        return req.subtract(new BigDecimal("0.02"));
    }

}

0 个答案:

没有答案