SpringBoot主鍵ID傳到前端后精度丟失的問題解決
簡介
本文用示例介紹SpringBoot如何解決雪花算法主鍵ID傳到前端后精度丟失問題。
問題描述
Java后端Long類型的范圍
-2^63~2^63,即:-9223372036854775808~9223372036854775807,它是19位的。
這個(gè)數(shù)字可以通過方法獲得:Long.MAX_VALUE、Long_MIN_VALUE。
前端JS的數(shù)字類型的范圍
-2^53~2^53,即:-9007199254740991~9007199254740991,它是16位的。
這個(gè)數(shù)字可以通過方法獲得:Number.MAX_SAFE_INTEGER、Number.MIN_SAFE_INTEGER。
結(jié)論
可見,Java后端的Long寬度大于前端的。雪花算法一般會生成18位或者19位寬度的數(shù)字,那么這時(shí)就會出問題。
項(xiàng)目場景
1.表結(jié)構(gòu)
主鍵類型是BIGINT,存儲雪花算法生成的ID。
CREATE TABLE `user` ( `id` BIGINT(32) NOT NULL COMMENT '用戶id', ... PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶表';
2.Entity
用Long 類型對應(yīng)數(shù)據(jù)庫ID的BIGINT類型。
這里使用 MybatisPlus 的雪花算法自動生成19位長度的純數(shù)字作為主鍵ID。(當(dāng)然也可以手動用雪花算法生成ID)
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.ASSIGN_ID)
private Long id;
//其他成員
}3.響應(yīng)給前端
以JSON數(shù)據(jù)響應(yīng)給前端正常
{
"id": 1352166380631257089,
...
}問題描述
實(shí)例
Controller
package com.knife.controller;
import com.knife.entity.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping("find")
public UserVO find(Long id) {
UserVO userVO = new UserVO();
userVO.setId(id);
userVO.setUsername("Tony");
return userVO;
}
}Entity
package com.knife.entity;
import lombok.Data;
@Data
public class UserVO {
private Long id;
private String username;
}測試
訪問:http://localhost:8080/user/find?id=1352213368413982722
結(jié)果

問題復(fù)現(xiàn)
從上邊可以看到,并沒有問題。
為什么沒有出問題?
前端傳入后端:SpingMVC會自動將String類型的ID轉(zhuǎn)為Long類型,不會出問題
后端響應(yīng)給前端:是JSON格式,與JS沒有關(guān)系,不會出問題
什么時(shí)候會出問題?
前端接收到JSON之后,將其序列化為JS對象,然后進(jìn)行其他操作。在JSON轉(zhuǎn)JS對象時(shí)就會出問題,如下:

可以看到,原來id為1352213368413982722,序列化為JS對象后變成了 1352213368413982700
代碼為:
const json = '{"id": 1352213368413982722, "name": "Tony"}';
const obj = JSON.parse(json);
console.log(obj.id);
console.log(obj.name);解決方案
有如下兩種方案
1.將數(shù)據(jù)庫表設(shè)計(jì)的id字段由 Long 類型改成 String 類型。
2.前端用String類型來保存ID保持精度,后端及數(shù)據(jù)庫繼續(xù)使用Long(BigINT)類型
方案1使用String 類型做數(shù)據(jù)庫ID,查詢性能會大幅度下降。所以應(yīng)該采用方案2。本文介紹方案2。
全局處理
簡介
自定義ObjectMapper。
方案1:ToStringSerializer(推薦)
package com.knife.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 全局配置序列化返回 JSON 處理
SimpleModule simpleModule = new SimpleModule();
// 將使用String來序列化Long類型
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}測試
訪問:http://localhost:8080/user/find?id=1352213368413982722

方案2:自定義序列化器(不推薦)
序列化器
package com.knife.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import java.io.IOException;
/**
* 超出 JS 最大最小值 處理
*/
@JacksonStdImpl
public class BigNumberSerializer extends NumberSerializer {
/**
* 根據(jù) JS Number.MAX_SAFE_INTEGER 與 Number.MIN_SAFE_INTEGER 得來
*/
private static final long MAX_SAFE_INTEGER = 9007199254740991L;
private static final long MIN_SAFE_INTEGER = -9007199254740991L;
/**
* 提供實(shí)例
*/
public static final BigNumberSerializer instance = new BigNumberSerializer(Number.class);
public BigNumberSerializer(Class<? extends Number> rawType) {
super(rawType);
}
@Override
public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {
// 超出范圍 序列化位字符串
if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) {
super.serialize(value, gen, provider);
} else {
gen.writeString(value.toString());
}
}
}ObjectMapper配置
package com.knife.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 全局配置序列化返回 JSON 處理
SimpleModule simpleModule = new SimpleModule();
// 將使用自定義序列化器來序列化Long類型
simpleModule.addSerializer(Long.class, BigNumberSerializer.instance);
simpleModule.addSerializer(Long.TYPE, BigNumberSerializer.instance);
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}測試
訪問:http://localhost:8080/user/find?id=1352213368413982722

局部處理
說明
在字段上加:@JsonSerialize(using= ToStringSerializer.class)。
實(shí)例
package com.knife.entity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
@Data
public class UserVO {
@JsonSerialize(using= ToStringSerializer.class)
private Long id;
private String username;
}測試
訪問:http://localhost:8080/user/find?id=1352213368413982722

到此這篇關(guān)于SpringBoot主鍵ID傳到前端后精度丟失的問題解決的文章就介紹到這了,更多相關(guān)SpringBoot主鍵ID精度丟失內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Failed to execute goal org...的解決辦法
這篇文章主要介紹了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)
這篇文章主要為大家分享了利用Java語言實(shí)現(xiàn)統(tǒng)計(jì)關(guān)鍵字在文檔中出現(xiàn)的次數(shù)的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05

