网络营销和网络推广,asp网站优化,中山h5网站建设,长沙工商注册网上登记问题描述
后端再给前端返回数据#xff0c;使用Long类型的时候存在精度丢失问题。
原因分析#xff1a;
分布式项目中广泛使用雪花算法生成ID作为数据库表的主键#xff0c;Long类型的雪花ID有19位#xff0c;而前端接收Long类型用的是number类型#xff0c;但是number…问题描述
后端再给前端返回数据使用Long类型的时候存在精度丢失问题。
原因分析
分布式项目中广泛使用雪花算法生成ID作为数据库表的主键Long类型的雪花ID有19位而前端接收Long类型用的是number类型但是number类型的精度只有16位。这就导致雪花ID传到前端会出现精度丢失。 解决方案
通过Jackson序列化转String
Configuration
public class SerializerConfig {/*** Long类型前端精度丢失问题序列化时转String* param mapperBuilder* return*/Beanpublic ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder mapperBuilder) {ObjectMapper build mapperBuilder.createXmlMapper(false).build();build.setSerializationInclusion(JsonInclude.Include.NON_NULL);SimpleModule module new SimpleModule();module.addSerializer(Long.class, ToStringSerializer.instance);module.addSerializer(Long.TYPE, ToStringSerializer.instance);build.registerModule(module);return build;}
}