一、继承 JsonSerializer 类,重写 serialize 抽象方法
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {@Overridepublic void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {if (ObjUtil.isNotNull(value)) {// 保留2位小数,四舍五入jsonGenerator.writeString(NumberUtil.decimalFormat("##0.00", value));} else {jsonGenerator.writeString(NumberUtil.decimalFormat("##0.00", 0.00));}}}
二、在响应属性标记注解 @JsonSerialize(using = BigDecimalSerializer.class)
@ApiModelProperty(value = "商品价格")@JsonSerialize(using = BigDecimalSerializer.class)private BigDecimal goodsPrice;