1.问题出现原因
问题出现的原因是JavaScript只能对16位以内长度字符可以保证精度如果超过则无法保持精度,例如后端返回给前端ID超过16位则会丢失精度
2.解决方案
后端在给前端返回数据ID时将Long类型转换为String字符串,如果前端拿到的是字符串则不会丢失精度,然后用户在根据此ID进行查询时,向后端发送的是字符串,那么后端对字符串进行解析转换成Long或者Integer就好了
3.具体实现
方案一
在具体ID上添加注解标记此ID使用JsonFormat序列化
public class user implements Serializable {
//方式一
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long id;
private String username;
private String password;
方案二
在具体ID上添加注解标记此ID使用JsonFormat序列化
public class user implements Serializable {
//方式二
@JsonSerialize(using = ToStringSerializer.class)
private String userId;
综上两种解决方案都需要初始化到IOC容器中(在SpringBoot启动类加上)
@Bean
public 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;