springboot中使用FastJson解決long類型在js中失去精度的問題
使用FastJson解決long類型在js中失去精度問題
1.pom中需要將默認(rèn)的jackson排除掉
<dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? <exclusions> ? ? ? ? <!-- json庫(kù)統(tǒng)一使用fastjson --> ? ? ? ? <exclusion> ? ? ? ? ? ? <groupId>com.fasterxml.jackson.core</groupId> ? ? ? ? ? ? <artifactId>jackson-databind</artifactId> ? ? ? ? </exclusion> ? ? </exclusions> </dependency>
2.利用fastJson替換掉jackson
package com.nightliar.bootdemo.config.spring;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.nightliar.bootdemo.interceptor.GlobalInterceptor;
import com.nightliar.bootdemo.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Nightliar
* 2018-08-15 11:09
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 添加攔截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry){
//全局?jǐn)r截器
registry.addInterceptor(new GlobalInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/static/**");
//登陸攔截器
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/static/**");
}
/**
* 利用fastJson替換掉jackson,且解決中文亂碼問題
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNonStringKeyAsString,
SerializerFeature.BrowserCompatible);
//解決Long轉(zhuǎn)json精度丟失的問題
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
//處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
springboot long精度缺失問題
?/**
? ? ?* 解決Jackson導(dǎo)致Long型數(shù)據(jù)精度丟失問題
? ? ?* @return
? ? ?*/
? ? @Bean("jackson2ObjectMapperBuilderCustomizer")
? ? public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
? ? ? ? Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
? ? ? ? ? ? ? ? jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
? ? ? ? ? ? ? ? ? ? ? ? .serializerByType(Long.TYPE, ToStringSerializer.instance);
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? return customizer;
}問題
jackson2ObjectMapperBuilderCustomizer不生效
主要是重復(fù)了
在WebMvcConfigurer中添加
@Autowired(required = false)
? ? private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
?
? ? @Override
? ? public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
? ? ? ? converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
? ? ? ? if (Objects.isNull(mappingJackson2HttpMessageConverter)) {
? ? ? ? ? ? converters.add(0, new MappingJackson2HttpMessageConverter());
? ? ? ? } else {
? ? ? ? ? ? converters.add(0, mappingJackson2HttpMessageConverter);
? ? ? ? }
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
- SpringBoot如何使用Fastjson解析Json數(shù)據(jù)
- springboot中用fastjson處理返回值為null的屬性值
- 使用SpringBoot+OkHttp+fastjson實(shí)現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過程解析
- SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
- springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過濾字段為null值如何解決
相關(guān)文章
Java的this關(guān)鍵字的使用與方法的重載相關(guān)知識(shí)
這篇文章主要介紹了Java的this關(guān)鍵字的使用與方法的重載相關(guān)知識(shí),是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
Java中回調(diào)函數(shù)?(callback)?及其實(shí)際應(yīng)用場(chǎng)景
在Java中回調(diào)函數(shù)(Callback)是一種常見的設(shè)計(jì)模式,用于實(shí)現(xiàn)異步操作或事件處理,這篇文章主要給大家介紹了關(guān)于Java中回調(diào)函數(shù)?(callback)?及其實(shí)際應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2024-02-02
Java面試之如何實(shí)現(xiàn)10億數(shù)據(jù)判重
當(dāng)數(shù)據(jù)量比較大時(shí),使用常規(guī)的方式來(lái)判重就不行了,所以這篇文章小編主要來(lái)和大家介紹一下Java實(shí)現(xiàn)10億數(shù)據(jù)判重的相關(guān)方法,希望對(duì)大家有所幫助2024-02-02
springboot中PostMapping正常接收json參數(shù)后返回404問題
這篇文章主要介紹了springboot中PostMapping正常接收json參數(shù)后返回404問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)二維數(shù)組與稀疏數(shù)組轉(zhuǎn)換詳解
稀疏數(shù)組是用于優(yōu)化,壓縮具有以下特點(diǎn)的二維數(shù)組:當(dāng)二維數(shù)組中的元素大部分相同,有意義的數(shù)據(jù)元素較少時(shí),可以使用稀疏數(shù)組進(jìn)行簡(jiǎn)化,節(jié)省存儲(chǔ)空間2021-10-10
PageHelper在springboot+mybatis框架中的使用步驟及原理解析
這篇文章主要介紹了PageHelper在springboot+mybatis框架中的使用步驟及原理解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Java基于二維數(shù)組實(shí)現(xiàn)的數(shù)獨(dú)問題示例
這篇文章主要介紹了Java基于二維數(shù)組實(shí)現(xiàn)的數(shù)獨(dú)問題,涉及java針對(duì)數(shù)組的遍歷、計(jì)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
淺談DetachedCriteria和Criteria的使用方法(必看)
下面小編就為大家?guī)?lái)一篇淺談DetachedCriteria和Criteria的使用方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05

