SpringBoot解决后台Long类型精度丢失

本文介绍两种解决后台传递Long类型数据精度丢失的方法:一是在启动类中使用@JsonComponent注解并自定义ObjectMapper;二是通过继承WebMvcConfigurerAdapter类配置自定义的ObjectMapper。两种方法均通过将Long类型转换为String类型来避免精度损失。

第一种方法

在启动类中加 @JsonComponent 注解

再加上以下代码

   @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        //忽略value为null 时 key的输出
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule module = new SimpleModule();
        module.addSerializer(Long.class, ToStringSerializer.instance);
        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(module);
        return objectMapper;
    }

第二种

在启动类继承  extends WebMvcConfigurerAdapter 

随后加上这段代码

 //解决后台传long类型精度丢失问题
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }
Spring Boot解决雪花算法前端精度丢失问题,主要是解决后端Long类型的雪花ID传输到前端后,因JS的Number数据类型精度限制而导致的精度丢失问题,有以下两种主要解决方案: ### 注解方式 在实体类的ID属性上添加`@JsonSerialize`注解,指定使用`ToStringSerializer.class`,这样在使用Jackson进行JSON序列化时,会将Long类型的ID转换为String类型。示例代码如下: ```java import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data; @Data public class Course { // 序列化时,不采用long类型,而采用string类型,防止雪花精度丢失问题 @JsonSerialize(using = ToStringSerializer.class) @TableId(type = IdType.ASSIGN_ID) // 主键生成策略,雪花 private Long id; private Long companyId; // 公司编号 private String companyName; // 公司名称 } ``` 这种方式适用于对单个实体类的ID属性进行处理,精准控制需要转换的ID字段 [^3][^4]。 ### 配置类实现 适用于全局配置,可在Spring Boot应用中统一将Long类型的ID转换为String类型。虽然引用中未给出具体代码,但思路是配置Jackson的序列化规则,让其在序列化所有Long类型的ID时都转换为String类型。这种方式可以避免在每个实体类的ID属性上都添加注解,提高开发效率 [^1]。 最终方案是前端使用String类型的雪花ID以保持精度,后端及数据库继续使用Long(BigINT)类型,这样既解决了前端精度丢失问题,又不影响数据库查询执行效率。前端将String类型的ID传回服务端时,Spring反序列化参数接收默认支持用Long类型接收 [^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯小胖 Mini胖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值