登录
注册
写文章
发现
工具
SpringBoot使用fastjson的JsonField注解序列化BigDecimal
_3t3lfz KEKfID
编辑文章
SpringBoot使用fastjson的JsonField注解序列化BigDecimal
asfx站长
2020.09.28 22:48:40
阅读
939
代码: ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>xyz.qiuyiping</groupId> <artifactId>fastjson-bigdecimal-serialize</artifactId> <version>0.0.1-SNAPSHOT</version> <name>fastjson-bigdecimal-serialize</name> <description>Spring Boot使用fastjson的JsonField注解序列化Bigdecimal</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.56</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` ```java import com.alibaba.fastjson.serializer.*; import java.text.DecimalFormat; /** * 自定义 BigDecimal 序列化 */ public class CustomerBigDecimalCodec extends BigDecimalCodec implements ContextObjectSerializer { public final static CustomerBigDecimalCodec instance = new CustomerBigDecimalCodec(); /** * 当BigDecimal类型的属性上有@JsonFiled注解,且该注解中的format有值时,使用该方法进行序列化,否则使用fastjson的 * BigDecimalCodec中的write方法进行序列化 */ @Override public void write(JSONSerializer serializer, Object object, BeanContext context){ SerializeWriter out = serializer.out; if(object == null) { out.writeString(""); return; } String format = context.getFormat(); DecimalFormat decimalFormat = new DecimalFormat(format); out.writeString(decimalFormat.format(object)); } } ``` ```java import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import com.example.sdk.qmall.serializer.CustomerBigDecimalCodec; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @Configuration public class HttpMessageConvertersConfig { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { //1.需要定义一个Convert转换消息的对象 FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter(); //2.添加fastjson的配置信息,比如是否要格式化返回的json数据 FastJsonConfig fastJsonConfig=new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect); //BigDecimal数据处理 SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance(); serializeConfig.put(BigDecimal.class, CustomerBigDecimalCodec.instance); fastJsonConfig.setSerializeConfig(serializeConfig); //3.在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //4.中文乱码 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); return new HttpMessageConverters(fastConverter); } } ``` ```java import com.alibaba.fastjson.annotation.JSONField; import java.math.BigDecimal; public class MoneyEntity { private BigDecimal noFormatMoney; @JSONField(format = "¥#0.00") private BigDecimal formatMoney; private String currencyType; public BigDecimal getNoFormatMoney() { return noFormatMoney; } public void setNoFormatMoney(BigDecimal noFormatMoney) { this.noFormatMoney = noFormatMoney; } public BigDecimal getFormatMoney() { return formatMoney; } public void setFormatMoney(BigDecimal formatMoney) { this.formatMoney = formatMoney; } public String getCurrencyType() { return currencyType; } public void setCurrencyType(String currencyType) { this.currencyType = currencyType; } } ``` ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; @RestController public class MoneyController { @GetMapping("/money") public Object money() { MoneyEntity moneyEntity = new MoneyEntity(); moneyEntity.setNoFormatMoney(new BigDecimal(123.45)); moneyEntity.setFormatMoney(new BigDecimal(67.89)); moneyEntity.setCurrencyType("人民币"); return moneyEntity; } } ``` 启动springboot项目,访问http://localhost:8080/money 测试结果: ``` { "currencyType":"人民币", "formatMoney":"¥67.89", "noFormatMoney":123.4500000000000028421709430404007434844970703125 } ```
我的主页
退出