关于springboot:实体类BigDecimal精度控制

65次阅读

共计 626 个字符,预计需要花费 2 分钟才能阅读完成。

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {public BigDecimalSerializer() { }

    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {if (value != null) {BigDecimal number = value.setScale(2, 4);
            gen.writeNumber(number);
        } else {gen.writeNumber(value);
        }

    }
}



class    A{

    @JsonSerialize(using = BigDecimalSerializer.class)
    @ApiModelProperty(value = "价格下限")
    private BigDecimal maxPrice;
}

正文完
 0