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

SpringBoot--解决BigDecimal传到前端后精度丢失问题,第1张

原文网址:SpringBoot--解决BigDecimal传到前端后精度丢失问题_IT利刃出鞘的博客-CSDN博客

简介

本文用示例介绍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的范围 范围没有限制,可以认为无限大、无限小可以通过如下代码验证:
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

结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1323370.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存