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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SWT(JFace)體驗(yàn)之StackLayout布局
SWT(JFace)體驗(yàn)之StackLayout布局實(shí)現(xiàn)代碼。2009-06-06TOMCAT內(nèi)存溢出及大小調(diào)整的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猅OMCAT內(nèi)存溢出及大小調(diào)整的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05詳解JAVA 線程-線程的狀態(tài)有哪些?它是如何工作的?
這篇文章主要介紹了詳解JAVA 線程的的相關(guān)資料,文中講解非常細(xì)致,源碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以參考下2020-06-06SpringBoot中異常處理實(shí)戰(zhàn)記錄
在我們實(shí)際項(xiàng)目開放中經(jīng)常需要我們處理很多的異常,如何在spring boot項(xiàng)目里面實(shí)現(xiàn)異常處理呢,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中異常處理的相關(guān)資料,需要的朋友可以參考下2022-05-05java之Timer和TimerTask簡單demo(分享)
下面小編就為大家?guī)硪黄猨ava之Timer和TimerTask簡單demo(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐
本文主要介紹了springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Mybatis-plus對(duì)單表操作的封裝實(shí)現(xiàn)
本文詳細(xì)介紹了MyBatis-Plus單表操作,包括自定義SQL、邏輯刪除、樂觀鎖、全局?jǐn)r截器和代碼生成器等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12詳解使用Spring?Data?repository進(jìn)行數(shù)據(jù)層的訪問問題
這篇文章主要介紹了使用Spring?Data?repository進(jìn)行數(shù)據(jù)層的訪問,抽象出Spring Data repository是因?yàn)樵陂_發(fā)過程中,常常會(huì)為了實(shí)現(xiàn)不同持久化存儲(chǔ)的數(shù)據(jù)訪問層而寫大量的大同小異的代碼,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06