1. 新建类 BigDecimalSerializer
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.Data;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @author xzk
* @Description Bigdecimal保留小数
*/
@Data
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (ObjectUtil.isEmpty(value)) {
return;
}
gen.writeString(value.setScale(4, RoundingMode.HALF_UP).toString());
}
}
2. 在bean上加注解
3.返回结果
标签:jackson,Bigdecimal,java,value,import,小数,fasterxml From: https://www.cnblogs.com/-xuzhankun/p/16735103.html