Java中Long類型傳入前端數(shù)值出錯問題
1.問題描述
做數(shù)據(jù)資產(chǎn)交易項目發(fā)現(xiàn),在列表點擊詳情時,沒有得到對應(yīng)的詳情頁面,從后端請求數(shù)據(jù)部分都為空
通過查看網(wǎng)絡(luò)請求看到,請求的data值時null,而不是對應(yīng)的詳情數(shù)據(jù),
查看請求,發(fā)現(xiàn)請求對應(yīng)的id并不是產(chǎn)品的id,最后幾位數(shù)值出現(xiàn)錯誤,之后在數(shù)據(jù)列表的響應(yīng)中發(fā)現(xiàn),數(shù)據(jù)的id也都是錯誤的,都是在第16位或者第17位進行了四舍五入,后續(xù)數(shù)字都為0。
之后找到的問題的根源,在mysql數(shù)據(jù)庫中,Java中的Long類型對應(yīng)其中的bigInt類型,支持20位,而前端接收Long類型的數(shù)值(超過16位)時,會對第16位進行四舍五入,17位及之后補0,因此造成數(shù)值出錯。
2.解決辦法
在通過查找后,找到了對應(yīng)的解決辦法,均為將后端的返回值中的id轉(zhuǎn)化位String類型,下列為具體解決辦法。
2.1 修改id的類型為String
這種方法并不是很好,因為id的Long類型,對應(yīng)數(shù)據(jù)庫中的bigInt類型,數(shù)據(jù)庫中一般都使用bigInt來表示id,因此這樣修改會導(dǎo)致之后會有很多地方需要修改,因此并不推薦
2.2 在id進行序列化時,以String形式序列化
這種方法并沒有上述方法的問題
2.2.1 使用StringBoot的Jackson依賴中包
- 單個數(shù)據(jù)
此處使用的為Jackson下的ToStringSerializer
@JsonSerialize(using = ToStringSerializer.class) private Long productId;
- 針對所有的Long類型數(shù)據(jù)(將所有的Long類型的數(shù)據(jù)改變的序列化方式)
通過添加配置類來解決這個問題
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 @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); SimpleModule simpleModule = new SimpleModule(); //Long類型----String simpleModule.addSerializer(Long.class, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }
2.2.2 使用FastJson
- 單個數(shù)據(jù)
此處使用的為FastJSON下的ToStringSerializer
@JSONField(serializeUsing = ToStringSerializer.class) private Long productId;
- 多個數(shù)據(jù)
通過重寫WebMvcConfigurer中的configureMessageConverters方法重新配置轉(zhuǎn)換器
import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.ToStringSerializer; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class CustomFastJsonConfig { @Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { //1.定義convert轉(zhuǎn)換消息的對象 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); //2.添加fastJson的配置信息 FastJsonConfig fastJsonConfig = new FastJsonConfig(); //3.設(shè)置Long為字符串 SerializeConfig serializeConfig = SerializeConfig.globalInstance; serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); fastJsonConfig.setSerializeConfig(serializeConfig); //4.在convert中添加配置信息. converter.setFastJsonConfig(fastJsonConfig); return converter; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java連接并操作Sedna XML數(shù)據(jù)庫的方法
這篇文章主要介紹了Java連接并操作Sedna XML數(shù)據(jù)庫的方法,較為詳細的說明了Sedna XML數(shù)據(jù)庫的原理與功能,并給出了基于java操作Sedna XML數(shù)據(jù)庫的方法,需要的朋友可以參考下2015-06-06SpringBoot中支持Https協(xié)議的實現(xiàn)
本文主要介紹了SpringBoot中支持Https協(xié)議的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01SpringBoot+Apache tika實現(xiàn)文檔內(nèi)容解析的示例詳解
Apache tika是Apache開源的一個文檔解析工具,本文主要為大家介紹了如何在springboot中引入tika的方式解析文檔,感興趣的小伙伴可以了解一下2023-07-07SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實現(xiàn)方法詳解
這篇文章主要介紹了SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12