Long类型精度丢失问题

1、现象

雪花算法得到的ID较长,传到前端后,精度丢失

2、解决方式

  • 方式一:使用@JsonSerialize注解
@JsonSerialize(using= ToStringSerializer.class)
private Long id;
  • 方式二:统一配置
@Bean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    objectMapper.registerModule(simpleModule);
    return objectMapper;
}

你可能感兴趣的:(踩坑记录,spring,boot,后端,java,Long精度丢失)