Java中Long類型傳入前端數(shù)值出錯(cuò)問(wèn)題
1.問(wèn)題描述
做數(shù)據(jù)資產(chǎn)交易項(xiàng)目發(fā)現(xiàn),在列表點(diǎn)擊詳情時(shí),沒(méi)有得到對(duì)應(yīng)的詳情頁(yè)面,從后端請(qǐng)求數(shù)據(jù)部分都為空
通過(guò)查看網(wǎng)絡(luò)請(qǐng)求看到,請(qǐng)求的data值時(shí)null,而不是對(duì)應(yīng)的詳情數(shù)據(jù),
查看請(qǐng)求,發(fā)現(xiàn)請(qǐng)求對(duì)應(yīng)的id并不是產(chǎn)品的id,最后幾位數(shù)值出現(xiàn)錯(cuò)誤,之后在數(shù)據(jù)列表的響應(yīng)中發(fā)現(xiàn),數(shù)據(jù)的id也都是錯(cuò)誤的,都是在第16位或者第17位進(jìn)行了四舍五入,后續(xù)數(shù)字都為0。
之后找到的問(wèn)題的根源,在mysql數(shù)據(jù)庫(kù)中,Java中的Long類型對(duì)應(yīng)其中的bigInt類型,支持20位,而前端接收Long類型的數(shù)值(超過(guò)16位)時(shí),會(huì)對(duì)第16位進(jìn)行四舍五入,17位及之后補(bǔ)0,因此造成數(shù)值出錯(cuò)。
2.解決辦法
在通過(guò)查找后,找到了對(duì)應(yīng)的解決辦法,均為將后端的返回值中的id轉(zhuǎn)化位String類型,下列為具體解決辦法。
2.1 修改id的類型為String
這種方法并不是很好,因?yàn)閕d的Long類型,對(duì)應(yīng)數(shù)據(jù)庫(kù)中的bigInt類型,數(shù)據(jù)庫(kù)中一般都使用bigInt來(lái)表示id,因此這樣修改會(huì)導(dǎo)致之后會(huì)有很多地方需要修改,因此并不推薦
2.2 在id進(jìn)行序列化時(shí),以String形式序列化
這種方法并沒(méi)有上述方法的問(wèn)題
2.2.1 使用StringBoot的Jackson依賴中包
- 單個(gè)數(shù)據(jù)
此處使用的為Jackson下的ToStringSerializer
@JsonSerialize(using = ToStringSerializer.class) private Long productId;
- 針對(duì)所有的Long類型數(shù)據(jù)(將所有的Long類型的數(shù)據(jù)改變的序列化方式)
通過(guò)添加配置類來(lái)解決這個(gè)問(wèn)題
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
- 單個(gè)數(shù)據(jù)
此處使用的為FastJSON下的ToStringSerializer
@JSONField(serializeUsing = ToStringSerializer.class) private Long productId;
- 多個(gè)數(shù)據(jù)
通過(guò)重寫(xiě)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)換消息的對(duì)象 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis代碼生成+自定義注解+自定義注釋實(shí)例
這篇文章主要介紹了mybatis代碼生成+自定義注解+自定義注釋實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java連接并操作Sedna XML數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Java連接并操作Sedna XML數(shù)據(jù)庫(kù)的方法,較為詳細(xì)的說(shuō)明了Sedna XML數(shù)據(jù)庫(kù)的原理與功能,并給出了基于java操作Sedna XML數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2015-06-06SpringBoot中支持Https協(xié)議的實(shí)現(xiàn)
本文主要介紹了SpringBoot中支持Https協(xié)議的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01SpringBoot+Apache tika實(shí)現(xiàn)文檔內(nèi)容解析的示例詳解
Apache tika是Apache開(kāi)源的一個(gè)文檔解析工具,本文主要為大家介紹了如何在springboot中引入tika的方式解析文檔,感興趣的小伙伴可以了解一下2023-07-07SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12