SpringBoot使用fastjson的JsonField注解序列化Bigdecimal
代码
<?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>
package xyz.qiuyiping.fastjson_bigdecimal_serialize.config;
import com.alibaba.fastjson.serializer.*;
import java.text.DecimalFormat;
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));
}
}
package xyz.qiuyiping.fastjson_bigdecimal_serialize.config;
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 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);
}
}
package xyz.qiuyiping.fastjson_bigdecimal_serialize.entity;
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;
}
}
package xyz.qiuyiping.fastjson_bigdecimal_serialize.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.qiuyiping.fastjson_bigdecimal_serialize.entity.MoneyEntity;
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
}
github代码地址:https://github.com/cainiaoqiu/fastjson-bigdecimal-serialize

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
数字对讲系统开发札记(前端linux c 后端 c#)
数字对讲系统开发札记(前端linux c 后端 c#)前言 数字化是一种趋势,特别是在“提速降费”的大环境下,这种趋势愈发明显。对讲机这种古老的系统也处在时代的变革之中,虽然手机的功能越来越强,让人怀疑对讲机是否还有存在的必要。诚然,对讲机仍然有它的市场。有时候,功能太多太强反而不是优势;对讲机的优势就是功能简单,专业性强。 笔者最近也涉足了对讲系统的开发,发现这个行业也大有可为。特别是4G、5G的出现,数字对讲系统的优势逐步显现;就像数码相机代替光学相机,模拟对讲机最终会被数字对讲机代替。虽然笔者接触数字对讲时间并不长;但是敏锐的觉察到,在这一细分市场做好、做强,定有丰厚回报。我把开发对讲系统的一些技术要点,想法写下来;希望大家多多交流指导!有志同道合者,可以联系我。 对讲系统现状 笔者是技术出身,对技术有一定的敏感性。对于市场,只能算作门外汉了;好在,技术人员推理能力比较强。对于对讲系统现状,我确实没有过多的发言权,我就根据了解的一些的情况,做一些合理的推理。模拟对讲系统出现已有几十年了,直到最近几年,数字对讲系统才有突飞猛进的发展,有加速替换模拟对讲系统的趋势。有几个原因:1流量...
-
下一篇
JAVA——Exception&Error
JAVA——Exception&Error在万物皆对象的JAVA中,先让我们看看Exception和Error的地位吧: avatar 可见Exception和Error都继承自Throwable类,所以二者既有相同也有不同之处。相同之处就是他们都是程序出错导致的,或说程序不正常运行才会实例化的类,不同之处就在于产生他们的的程序的出错原因不同,下面就来详细比较一下。 一,Exception&Error从命名来看Error貌似比Exception要严重一点,事实确实如此。Error产生的情况一般是指不大可能出现且不可预料的情况,如JVM本身不正常,内存溢出,栈溢出等一些比较严重,会使程序终止的情况。而Exception通常是一些可以预料的不好的情况,如类型转换失败,多线程数据插入失败等,虽然程序出错,但可预料,一般也不会使程序终止。 二,未受检查异常和受检查异常1,Error和Exception的子类RuntimeException都是未受检查的异常,也可以叫运行时异常,因为它们都是不可预料的,在程序运行时才会出现的异常。 2,Exception的其他子类是受检查的异常,...
相关文章
文章评论
共有0条评论来说两句吧...