SpringBoot返回前端Long類型字段丟失精度問題及解決方案
一、問題分析
Java服務(wù)端返回Long整型數(shù)據(jù)給前端,JS會(huì)自動(dòng)轉(zhuǎn)換為Number類型。而Long類型能表示的最大值為(
),當(dāng)數(shù)值超過JS中Number類型的最大值(
)時(shí),就會(huì)丟失精度。
二、解決方案
1、返回給前端的屬性類型設(shè)為String類型。
2、配置Jackson序列化
首先,引入依賴。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.12.1</version>
</dependency>新建一個(gè)自定義大數(shù)據(jù)序列化類,如下:
@Slf4j
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
// 全局配置序列化返回 JSON 處理
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
builder.modules(javaTimeModule);
builder.timeZone(TimeZone.getDefault());
log.info("初始化 jackson 配置");
};
}
}新建Jackson配置類
@Slf4j
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
// 全局配置序列化返回 JSON 處理
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
builder.modules(javaTimeModule);
builder.timeZone(TimeZone.getDefault());
log.info("初始化 jackson 配置");
};
}
}三、總結(jié)
本文針對(duì)Java服務(wù)端返回Long整型數(shù)據(jù)給前端時(shí)精度丟失問題,提出了兩種解決方案,大家根據(jù)自己的需求選擇食用
到此這篇關(guān)于SpringBoot返回前端Long類型字段丟失精度問題及解決方案的文章就介紹到這了,更多相關(guān)SpringBoot返回Long丟失精度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot傳給前端Long類型精度丟失的解決方案
- SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問題
- SpringBoot分頁的實(shí)現(xiàn)與long型id精度丟失問題的解決方案介紹
- springboot?Long?精度丟失問題解決
- SpringBoot2.0解決Long型數(shù)據(jù)轉(zhuǎn)換成json格式時(shí)丟失精度問題
- SpringBoot全局配置long轉(zhuǎn)String丟失精度的問題解決
- SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
- SpringBoot基于Jackson解決Long型長(zhǎng)度丟失問題
相關(guān)文章
java正則匹配HTML中a標(biāo)簽里的中文字符示例
這篇文章主要介紹了java正則匹配HTML中a標(biāo)簽里的中文字符,涉及java中文正則及HTML元素操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Java?list如何實(shí)現(xiàn)將指定元素排在第一位
這篇文章主要為大家詳細(xì)介紹了Java?list中如何實(shí)現(xiàn)將指定元素排在第一位,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
一文詳解如何使用線程池來優(yōu)化我們的應(yīng)用程序
線程池是一種工具,但并不是適用于所有場(chǎng)景。在使用線程池時(shí),我們需要根據(jù)應(yīng)用程序的性質(zhì)、計(jì)算資源的可用性和應(yīng)用程序的需求進(jìn)行適當(dāng)?shù)呐渲谩1疚闹饕榻B了如何使用線程池來優(yōu)化我們的應(yīng)用程序,需要的可以參考一下2023-04-04
在Spring?Boot使用Undertow服務(wù)的方法
Undertow是RedHAT紅帽公司開源的產(chǎn)品,采用JAVA開發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認(rèn)Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧2023-05-05

