首页 > 其他分享 >SpringBoot解决BigDecimal传到前端后精度丢失问题

SpringBoot解决BigDecimal传到前端后精度丢失问题

时间:2022-09-07 16:33:19浏览次数:69  
标签:SpringBoot value class 丢失 序列化 public objectMapper BigDecimal

1、局部处理

(1)在相应字段上加@JsonFormat
@JsonFormat(shape = JsonFormat.Shape.STRING)

(2)在相应字段上加@JsonSerialize
@JsonSerialize(using= ToStringSerializer.class)

 

2、全局处理

(1)ToStringSerializer
配置类
@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // 全局配置序列化返回 JSON 处理
        SimpleModule simpleModule = new SimpleModule();
        // 将使用String来序列化BigDecimal类型
        simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
(2)自定义序列化器
//自定义序列化类
@JacksonStdImpl
class BigDecimalToStringSerializer extends ToStringSerializer {
    public final static BigDecimalToStringSerializer instance = new BigDecimalToStringSerializer();
  
    public BigDecimalToStringSerializer() {
        super(Object.class);
    }
  
    public BigDecimalToStringSerializer(Class<?> handledType) {
        super(handledType);
    }
  
    @Override
    public boolean isEmpty(SerializerProvider prov, Object value) {
        if (value == null) {
            return true;
        }
        String str = ((BigDecimal) value).stripTrailingZeros().toPlainString();
        return str.isEmpty();
    }
  
    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider)
            throws IOException {
        gen.writeString(((BigDecimal) value).stripTrailingZeros().toPlainString());
        // 如果要求所有BigDecimal保留两位小数,可以这么写:
        // gen.writeString(((BigDecimal) value).setScale(2, RoundingMode.HALF_UP)
        //         .stripTrailingZeros().toPlainString());
    }
  
    @Override
    public void serializeWithType(Object value, JsonGenerator gen,
                                  SerializerProvider provider, TypeSerializer typeSer)
            throws IOException {
        // no type info, just regular serialization
        serialize(value, gen, provider);
    }
}

//配置类
@Configuration
public class JacksonConfig {
  
    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
  
        // 全局配置序列化返回 JSON 处理
        SimpleModule simpleModule = new SimpleModule();
        // 将使用String来序列化BigDecimal类型
        simpleModule.addSerializer(BigDecimal.class, BigDecimalToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}

参考:https://www.jb51.net/article/250548.htm

标签:SpringBoot,value,class,丢失,序列化,public,objectMapper,BigDecimal
From: https://www.cnblogs.com/wongzzh/p/16666295.html

相关文章

  • springboot集成hibernate-validator
    一、项目搭建1、使用springboot搭建一个web工程建web工程,不使用骨架创建maven的Java工程即可,不需要创建maven的web工程。2、添加父工程坐标和添加web启动器<parent>......
  • SpringBoot使用自定义注解+AOP+Redis实现接口限流
    为什么要限流系统在设计的时候,我们会有一个系统的预估容量,长时间超过系统能承受的TPS/QPS阈值,系统有可能会被压垮,最终导致整个服务不可用。为了避免这种情况,我们就需要对......
  • SpringBoot常用注解
    SpringBoot常用注解1.@SpringBootApplicationspringBoot的基石,启动类@Configuration应许spring注册额外的bean或者导入其他配置类@EnableAutoConfiguration启用Sp......
  • Springboot定义全局异常类详解
    前言当我们在开发过程中,会因为一些异常程序出现500,如果直接显示给客户看,这样很不友好。并且对我们后期维护,排查bug很困难。准备1.创建一个SpringBoot项目,引入web依赖,......
  • springboot官方文档解读
    官网地址:https://docs.spring.io/spring-boot/docs/2.7.3/reference/htmlsingle/1第一个springboot项目我们在一个路径下面创建pom.xml文件<?xmlversion="1.0"encod......
  • [安装配置] SpringBoot项目部署
    打包SpringBoot项目 部署方式一:手动部署1、将打包好的jar包上传到Linux服务器中mkdir-p/opt/java62/app2、前台启动SpringBoot应用编译jar包:java-jarhellowor......
  • SpringBoot-整合Druid
    1.添加jar包<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency>......
  • 14.Springboot多环境配置2
    1.主配置文件application.ymlspring:profiles:active:@profile.active@#需要在pom文件中指定变量#active:pro#include:mvcgroup:"pro......
  • MBR硬盘丢失UEFI启动项的问题
    MBR硬盘丢失UEFI启动项的问题现象描述:联想笔记本电脑,MBR硬盘有一个FAT32分区,UEFI启动的时候,UEFI的引导项bootx64.efi,bootmgfw.efi总是时不时地丢失。解决办法:用傲梅分区助......
  • springboot聚合项目搭建
    springboot聚合项目搭建1、简介1.1、什么是聚合项目?一个项目中包含多个子项目的项目。结构:|-父模块---|子模块1---|子模块2---|子模块31.2、聚合项目有什么......