Long类型数据前后端交互时丢失精度的理解:

Long类型数据前后端交互时丢失精度的理解:

          因为JavaScript处理数字的精度是有限的,有效精度为2的53次方,而数据库中的BigInt类型即Long类型的精度为2的63次方,所以在前端会将数据的精度丢失。

解决办法:

1,在前端接收数据时,将其用String类型表示并使用(处计算外)

                  

2,序列化——@JsonSerialize注解,序列化——toStringSerialize.class

               

3,增加对象转换器,在后端进行转换

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

    @Bean

    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        ObjectMapper mapper = new ObjectMapper();

        //日期格式转换

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        mapper.setDateFormat(new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN));

        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        //Long类型转String类型

        SimpleModule simpleModule = new SimpleModule();

        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);

        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

        mapper.registerModule(simpleModule);

        converter.setObjectMapper(mapper);

        return converter;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值