出现这种问题的原因是,前端JS导致的id精度丢失的问题,
前端JSON字符串转js对象,接收Long类型的是Number,Number精度是16位(雪花ID是19位),JS的Number数据类型导致精度丢失。
这个问题是在利用雪花算法生成id后,传给前端,当前端将这个长度超出自己处理范围(自己仅能处理16位的)的19位id向后台传的过程中,发生了精度丢失。
那么看到一位优秀的博主把问题说的很好,我这里就部分借鉴一下,原文链接在这里:
雪花算法⽣成ID传到前端之后精度丢失问题_锦年JNian的博客-CSDN博客_雪花算法生成id精度丢失
首先第一个办法就是:只需要让js处理的ID数据类型为字符串类型即可, 这样就不会损失精度了
解决办法一:直接使⽤@JsonSerialize注解
JsonSerialize注解可以帮我们实现字段值的序列化和反序列话
这样后端向前端返回式,对象转json的过程中,会将long类型的id,变成字符串进行返回,那么这样前端处理的就是字符串了,那么自然也就不存在精度的丢失了。
第二个办法就是自己配置:
解决办法二:
package com.itheima.reggie.common;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
public class JacksonObjectMapper extends ObjectMapper {
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);
//自定义转换规则
SimpleModule simpleModule = new SimpleModule()
.addSerializer(BigInteger.class, ToStringSerializer.instance)//将BigInteger转换为String
.addSerializer(Long.class, ToStringSerializer.instance);//将Long转换成String
this.registerModule(simpleModule);
}
}
这是第一步,创建一个类,继承ObjectMapper,做以上处理。
第二步,就是在springmvc的配置类中(也就是实现了WebMvcConfigurer接口的那个配置类) ,进行装配:
package com.itheima.reggie.config;
import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
//web相关配置
@Configuration
public class ReggieWebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginCheckInterceptor loginCheckInterceptor;
//设置静态资源映射
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问请求是/backend/**时,去classpath:/backend/寻找对应资源
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// //这里要拦截未登录的情况下,对系统的非静态资源请求
List urlList = new ArrayList<>();
urlList.add("/backend/**");
urlList.add("/error");
urlList.add("/employee/login");//管理系统登录请求
urlList.add("/employee/logout");//管理系统退出请求
registry.addInterceptor(loginCheckInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(urlList);
}
//扩展mvc框架的消息转换器 为了id精度损失 这一段是重点,其他可以不看
public void extendMessageConverters(List> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//设置对象转换器,底层使用Jackson将Java对象转为json
messageConverter.setObjectMapper(new JacksonObjectMapper());
//将上面的消息转换器对象追加到mvc框架的转换器集合中
converters.add(0, messageConverter);
}
}
这样,也可以达到 “后端向前端返回式,对象转json的过程中,会将long类型的id,变成字符串进行返回,那么这样前端处理的就是字符串了,那么自然也就不存在精度的丢失了”的效果!!
那么这第二种方法,也可以直接合并到springmvc的配置类中(也就是实现了WebMvcConfigurer接口的那个配置类)
解决办法三:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
// 全局配置序列化返回 JSON 处理
SimpleModule simpleModule = new SimpleModule();
// JSON Long ==> String 将所有的 Long类型 转换成 String类型 返回
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}
}
今天也是棒棒的一天!!!



