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

这篇文章将通过示例详细为大家介绍SpringBoot如何解决BigDecimal传到前端后精度丢失问题,文中的示例代码讲解详细,感兴趣的可以了解一下

简介

本文用示例介绍SpringBoot如何解决BigDecimal传到前端后精度丢失问题。

问题描述

实例

Controller

package com.knife.controller;
 
import com.knife.entity.UserVO;
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;
 
@RestController
@RequestMapping("user")
public class UserController {
 
    @GetMapping("save")
    public UserVO save(BigDecimal amount) {
        UserVO userVO = new UserVO();
        userVO.setId(1L);
        userVO.setUsername("Tony");
        userVO.setAmount(amount);
 
        return userVO;
    }
}

Entity

package com.knife.entity;
 
import lombok.Data;
 
import java.math.BigDecimal;
 
@Data
public class UserVO {
    private Long id;
 
    private String username;
 
    private BigDecimal amount;
}

测试

访问:http://localhost:8080/user/save?amount=12345671234567.1234

结果

问题复现

场景描述

实际项目中前端会这样处理:调用后端接口获得JSON格式的响应字符串,然后将JSON字符串解析为JavaScript对象(用于展示到对应的位置、方便计算等)。

前端调后端的写接口(增删改)时,会将JavaScript对象序列化为JSON格式的字符串,然后将其作为参数请求后端接口。

 实例1:精度丢失

const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.12345}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 12345671234567.123
console.log(JSON.stringify(obj));  // {"id":1,"name":"Tony","amount":12345671234567.123}

可以看到,在将json字符串转为JavaScript对象后,“amount” 丢失了精度。

实例2:丢失小数位

const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.00000}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 12345671234567
console.log(JSON.stringify(obj));  // {"id":1,"name":"Tony","amount":12345671234567}

可以看到,在将json字符串转为JavaScript对象后,“amount” 丢失了小数。

其他示例

const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.12345}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 12345671234567.123
 
const json = '{"id": 1, "name": "Tony", "amount": 123456712345678.12345}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 123456712345678.12
 
const json = '{"id": 1, "name": "Tony", "amount": 98765432198765.12345}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 98765432198765.12
 
const json = '{"id": 1, "name": "Tony", "amount": 987654321987654321.12345}';
const obj = JSON.parse(json);
console.log(obj.amount);  // 987654321987654300

Java后端BigDecimal的范围

1.范围没有限制,可以认为无限大、无限小

2.可以通过如下代码验证:

package com.example.a;
 
import java.math.BigDecimal;
 
public class Demo {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(
                "1234567890123456789012345678901234567890"
                        + "1234567890123456789012345678901234567890"
                        + ".123456789"
        );
        System.out.println(bigDecimal);
    }
}

执行结果:

12345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789

解决方案

把BigDecimal的序列化值改成字符串类型即可。

方案1:全局处理

法1:ToStringSerializer

配置类

package com.knife.config;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 
import java.math.BigDecimal;
 
@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;
    }
}

测试

访问:http://localhost:8080/user/save?amount=12345671234567.1234

结果:

法2:自定义序列化

自定义序列化器

package com.knife.config;
 
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
 
@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);
    }
}

配置类

package com.knife.config;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 
import java.math.BigDecimal;
 
@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;
    }
}

测试

访问:http://localhost:8080/user/save?amount=12345671234567.1234

结果:

方案2:局部处理

法1:@JsonSerialize

在相应字段上加此注解:

@JsonSerialize(using= ToStringSerializer.class)

示例

package com.knife.entity;
 
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
 
import java.math.BigDecimal;
 
@Data
public class UserVO {
    private Long id;
 
    private String username;
 
    @JsonSerialize(using= ToStringSerializer.class)
    private BigDecimal amount;
}

测试

访问:http://localhost:8080/user/save?amount=12345671234567.1234

结果:

法2:@JsonFormat

在相应字段上加此注解:

@JsonFormat(shape = JsonFormat.Shape.STRING)

示例

package com.knife.entity;
 
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
 
import java.math.BigDecimal;
 
@Data
public class UserVO {
    private Long id;
 
    private String username;
 
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private BigDecimal amount;
}

测试

访问:http://localhost:8080/user/save?amount=12345671234567.1234

结果:

到此这篇关于SpringBoot解决BigDecimal传到前端后精度丢失问题的文章就介绍到这了,更多相关SpringBoot BigDecimal精度丢失内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

本文标题为:SpringBoot解决BigDecimal传到前端后精度丢失问题