问题:
数据库中id是1465396696468033540 结果传到前端变成1465396696468033500,后面几位精度缺失了
原因:
Number精度是16位(雪花ID是19位的),so:JS的Number数据类型导致的精度丢失
解决办法:
/** * 防止精度丢失 */ @JsonSerialize(using = ToStringSerializer.class) private Long id;
@Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 全局配置序列化返回 JSON 处理 SimpleModule simpleModule = new SimpleModule(); //JSON Long ==> String simpleModule.addSerializer(Long.class, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }
原文入口:https://www.cnblogs.com/zimug/archive/2020/08/25/13557662.html
原文:https://www.cnblogs.com/heikedeblack/p/14333812.html