目录
2. 注解处理类DecimalPrecisionHandler
此文章需要先看Jackson自定义序列化注解 (支持同时使用多个自定义序列化注解)
-
注解类
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.math.RoundingMode; /** * 保留小数位数 (默认2位小数 四舍五入) * * @author ds */ @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @JacksonAnnotationsInside @JacksonBaseSerialize(handlerClazz = DecimalPrecisionHandler.class) public @interface DecimalPrecision { /** * 保留小数位数 (默认2位小数) */ int precision() default 2; /** * 舍入模式 (默认四舍五入) */ RoundingMode roundingMode() default RoundingMode.HALF_UP; /** * 是否单独返回 (默认否) */ boolean isSeparately() default false; /** * 是否去除末尾的0 (默认是) */ boolean isStripTrailingZeros() default true; }
-
注解处理类DecimalPrecisionHandler
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; /** * Jackson 序列化处理类 * 保留小数位数 (默认2位小数 四舍五入) * * @author ds */ public class DecimalPrecisionHandler implements IJacksonBaseSerializeHandler<DecimalPrecision, Object> { /** * Method that can be called to ask implementation to serialize * values of type this serializer handles. * * @param value Value to serialize; can <b>not</b> be null. * @param gen Generator used to output resulting Json content * @param serializers Provider that can be used to get serializers for * serializing Objects value contains, if any. */ @Override public void serializeHandler(BeanProperty beanProperty, DecimalPrecision annotation, Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { // 为空直接跳过 if (beanProperty == null) { return; } BigDecimal temp = this.handlerBigDecimal(value); int precision = annotation.precision(); RoundingMode roundingMode = annotation.roundingMode(); temp = temp.setScale(precision, roundingMode); // 是否去除末尾的0 (默认是) if (annotation.isStripTrailingZeros()) { temp = temp.stripTrailingZeros(); } String valueStr = temp.toPlainString(); if (annotation.isSeparately()) { // 单独返回 gen.writeStringField(beanProperty.getName() + "DecimalPrecision", valueStr); } else { // 当前字段中返回 gen.writeStringField(beanProperty.getName(), valueStr); } } /** * 处理成BigDecimal类型 * * @param value Value to serialize; can <b>not</b> be null. * @return 对应的BigDecimal类型实例 */ private BigDecimal handlerBigDecimal(Object value) { BigDecimal temp; if (value instanceof BigDecimal) { temp = (BigDecimal) value; } else if (value instanceof Integer) { temp = BigDecimal.valueOf((Integer) value); } else if (value instanceof Long) { temp = BigDecimal.valueOf((Long) value); } else if (value instanceof BigInteger) { temp = new BigDecimal((BigInteger) value); } else if (value instanceof String) { temp = new BigDecimal((String) value); } else { throw new NumberFormatException("No digits found."); } return temp; } }