java?Long類型轉(zhuǎn)為json后數(shù)據(jù)損失精度的處理方式
最近在項目開發(fā)中,使用spring boot+mybatis的架構(gòu),數(shù)據(jù)庫設(shè)計主鍵id時定義為bigint類型,使用mybatis的自動生成代碼后沒注意,主鍵在pojo里的類型為Long。查詢時獲取的對象列表取出的數(shù)值沒有問題,但轉(zhuǎn)為json傳到前端后,id的數(shù)據(jù)始終不是數(shù)據(jù)庫查出來的那個。
數(shù)據(jù)庫表結(jié)構(gòu)設(shè)計

AbumTip類

根據(jù)外鍵abum_id在數(shù)據(jù)庫中查詢的結(jié)果

Controller查到的結(jié)果

chrome瀏覽器preview結(jié)果

可以看到abumId(對應表abum_id)和tipId(對應表中tip_id)查詢到的Long類型的數(shù)據(jù)都不對。
解決的方法
方法一
重新生成pojo對象,將所有數(shù)據(jù)庫類型為bigint都映射成String類型。
方法二
對于使用springboot,則增加配置代碼:
package com.gj.app.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.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
@EnableWebMvc
@Configuration
public class WebDataConvertConfig extends WebMvcConfigurerAdapter {
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
/**
* 序列換成json時,將所有的long變成string
* 因為js中得數(shù)字類型不能包含所有的java long值
*/
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}
}方法三
在spring MVC中
增加類:
其中LongToStringJsonConverter為自定義轉(zhuǎn)換器
public class LongToStringJsonConverter extends ObjectMapper {
/**
*
*/
private static final long serialVersionUID = 1683531771040674386L;
@Override
public ObjectMapper registerModule(Module module) {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
return super.registerModule(simpleModule);
}
} <mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<!-- <bean class="com.fasterxml.jackson.databind.ObjectMapper"> -->
<bean class="mypackage.LongToStringAdapter">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java與C++實現(xiàn)相同的MD5加密算法簡單實例
下面小編就為大家?guī)硪黄狫ava與C++實現(xiàn)相同的MD5加密算法簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
關(guān)于logback日志級別動態(tài)切換的四種方式
這篇文章主要介紹了關(guān)于logback日志級別動態(tài)切換的四種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于Java實現(xiàn)HttpServer模擬前端接口調(diào)用
這篇文章主要介紹了關(guān)于Java實現(xiàn)Http?Server模擬前端接口調(diào)用,Http?協(xié)議是建立在?TCP?協(xié)議之上的協(xié)議,所以能用?TCP?來自己模擬一個簡單的?Http?Server?當然是可以的,需要的朋友可以參考下2023-04-04

