我已将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"));
}
}