一、需求
使用@JsonSerialize,来解决单位换算,以及数据为空时返回“**”默认值问题。例如有时候我们需要将金额,以万元的形式返回,需要千分位等等。
注意:如果使用fastjson进行序列化,则本注解不会生效。此注解是jackson包下的。
二、pom依赖
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version></dependency>
三、通用序列化类定义
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;/*** @Author:* @Description 保留一位小数,且以千分位展示数据 例如:3,000.8* @Date: 下午4:44 2024/01/15*/
@Slf4j
public class BigDecimalBaseSerializer extends JsonSerializer<Object> {private String format = "#,##0.0";private BigDecimal unit = BigDecimal.ONE;private DecimalFormat df = new DecimalFormat(format);private Integer scaleSize = 1;public static final String LINE_STR = "--";public BigDecimalBaseSerializer() {}public BigDecimalBaseSerializer(String format, BigDecimal unit, Integer scaleSize) {if (format != null) {this.format = format;this.df = new DecimalFormat(this.format);}if (unit != null) {this.unit = unit;}if (scaleSize != null) {this.scaleSize = scaleSize;}}@Overridepublic void serialize(Object object, JsonGenerator gen, SerializerProvider serializer) throws IOException {BigDecimal val = toBigDecimal(object);if (val != null) {String str = bigDecimalToStr(val);gen.writeString(str);} else {gen.writeString(LINE_STR);}}public String bigDecimalToStr(BigDecimal val) {return toString(val, df, this.unit, this.scaleSize);}public String toString(BigDecimal val, DecimalFormat df, BigDecimal unit, Integer scaleSize) {if (BigDecimal.ZERO.equals(val)) {return df.format(val);} else if (unit != null) {val = val.divide(unit, 10, BigDecimal.ROUND_HALF_UP);val = val.setScale(scaleSize, BigDecimal.ROUND_HALF_UP);return df.format(val);}return LINE_STR;}public BigDecimal toBigDecimal(Object object) {BigDecimal res = null;if (object != null) {if (object instanceof BigDecimal) {res = ((BigDecimal) object);} else if (object instanceof String) {String str = (String) object;if (StringUtils.isNotBlank(str)) {res = new BigDecimal(str);}} else if (object instanceof Integer) {Integer integer = (Integer) object;res = new BigDecimal(integer);} else if (object instanceof Double) {Double doubleVal = (Double) object;res = BigDecimal.valueOf(doubleVal);} else if (object instanceof Long) {Long longVal = (Long) object;res = new BigDecimal(longVal);} else {log.warn("BigDecimal转换异常{}", object.getClass().getTypeName());}}return res;}
}
四、以万为单位返回,保留一位小数
import java.math.BigDecimal;/*** @Author:* @Description 以万为单位返回,保留一位小数* @Date: 下午4:58 2024/01/15*/
public class TenThousandSerialize extends BigDecimalBaseSerializer {private static BigDecimal divisor = new BigDecimal(10000);public TenThousandSerialize() {super(null, divisor, null);}
}
五、以万为单位返回,取整数
/*** @Author:* @Description 以万为单位返回,取整数* @Date: 下午4:58 2024/01/15*/
public class TenThousandIntegerSerializer extends BigDecimalBaseSerializer {private static Integer scaleSize = 0;private static BigDecimal divisor = new BigDecimal(10000);private static String pattern = "#,##0";public TenThousandIntegerSerializer() {super(pattern, divisor, scaleSize);}
}
六、构建实体类
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;import java.math.BigDecimal;/*** @Author: * @Description* @Date: 下午2:15 2024/01/15*/
@Data
public class EntityData {private String strNum;@JsonSerialize(using = TenThousandIntegerSerializer.class, nullsUsing = FieldNullSerializer.class)private BigDecimal bdNum;@JsonSerialize(using = BigDecimalBaseSerializer.class, nullsUsing = FieldNullSerializer.class)private BigDecimal bdNum1;@JsonSerialize(nullsUsing = FieldNullSerializer.class)private BigDecimal bdNum2;}
七、测试
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;/*** @Author: * @Description* @Date: 下午2:37 2024/1/15*/
@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/111")public Object test1() {EntityData data = new EntityData();data.setStrNum(null);data.setBdNum(BigDecimal.valueOf(1101010.23));data.setBdNum1(BigDecimal.valueOf(1101010.23));return data;}}
八、结果
{“strNum”:null,“bdNum”:“110”,“bdNum1”:“1,101,010.2”,“bdNum2”:“**”}