前端Long类型精度丢失:后端处理策略

avatar
作者
筋斗云
阅读量:0

文章目录


在这里插入图片描述

开发商城管理系统的品牌管理界面时,发现一个问题,接口返回品牌Id和页面展示的品牌Id不一致,如接口返回的是1816714744603811841,前端战胜的是1816714744603811800

这是因为在前端出现了数据精度丢失。

精度丢失的具体原因

JavaScript的Number类型用于表示浮点数,它遵循IEEE 754标准中的64位浮点数格式。这意味着它能够准确表示从(-2{53})到(2{53}-1)之间的所有整数。超出这个范围的整数值在转换为Number类型时可能会发生精度丢失,即原本不同的长整数会被转换成相同的浮点数值,从而导致数据失真。

解决方法

为了解决品牌ID在前后端传输过程中精度丢失的问题,可以采用以下几种解决方法:

1. 使用 @JsonSerializeToStringSerializer

在Java后端中,可以通过使用Jackson库的注解功能,将Long类型的字段在序列化为JSON时转换为String类型。这样,前端接收到的数据是字符串形式,避免了精度丢失的问题。

示例代码:

import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data;  @Data public class BrandVo {     // 使用ToStringSerializer将Long类型的id字段转换为String类型     @JsonSerialize(using = ToStringSerializer.class)     private Long id;     // 其他字段... } 

通过这种方式,我们可以确保后端返回的JSON中,Long类型的字段都以字符串的形式存在,前端可以直接将其作为字符串处理,无需担心精度问题。

2. 使用 @JsonFormat 注解

除了使用 ToStringSerializer,Jackson还提供了 @JsonFormat 注解,它允许指定字段的序列化格式。当将 shape 属性设置为 JsonFormat.Shape.STRING 时,Long类型的字段会被格式化为字符串。

示例代码:

import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data;  @Data public class BrandVo {     // 使用@JsonFormat注解将Long类型的id字段格式化为String     @JsonFormat(shape = JsonFormat.Shape.STRING)     private Long id;     // 其他字段... } 

这种方法同样可以确保Long类型的字段在序列化为JSON时以字符串形式出现,避免前端精度丢失的问题。

3. 全局配置解决方案

虽然使用注解可以在一定程度上解决问题,但对于大型项目,逐个字段添加注解不仅繁琐,还可能导致代码冗余和难以维护。因此,可以考虑使用全局配置的方式,一次性解决所有Long类型字段的序列化问题。

示例代码:

import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;  @Configuration public class JacksonConfig {      @Bean     @Primary     @ConditionalOnMissingBean(ObjectMapper.class)     public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {         ObjectMapper objectMapper = builder.createXmlMapper(false).build();         SimpleModule simpleModule = new SimpleModule();         // 将Long类型序列化为String类型         simpleModule.addSerializer(Long.class, ToStringSerializer.instance);         objectMapper.registerModule(simpleModule);         return objectMapper;     } } 

在这个配置中,我们创建了一个自定义的ObjectMapper Bean,并注册了一个SimpleModule,该模块使用ToStringSerializer将Long类型序列化为String类型。这样,整个应用中所有Long类型的字段在序列化时都会自动转换为String类型。

结论

前端精度丢失问题是一个常见的挑战,但通过上述三种方法,我们可以有效地解决这个问题。在实际开发中,我们可以根据项目的具体情况和需求选择合适的方法。对于需要精确表示大数字的场景,将Long类型转换为String类型是一个简单而有效的解决方案。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!