最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot中使用FastJson解決long類(lèi)型在js中失去精度的問(wèn)題

 更新時(shí)間:2022年06月15日 11:16:20   作者:Nightliar  
這篇文章主要介紹了springboot中使用FastJson解決long類(lèi)型在js中失去精度的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用FastJson解決long類(lèi)型在js中失去精度問(wèn)題

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,且解決中文亂碼問(wèn)題
     */
    @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精度丟失的問(wèn)題
        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);
        //處理中文亂碼問(wèn)題
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }
}

springboot long精度缺失問(wèn)題

?/**
? ? ?* 解決Jackson導(dǎo)致Long型數(shù)據(jù)精度丟失問(wèn)題
? ? ?* @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;
}

問(wèn)題

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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中finally關(guān)鍵字對(duì)返回值的影響詳解

    Java中finally關(guān)鍵字對(duì)返回值的影響詳解

    這篇文章主要介紹了Java中finally關(guān)鍵字對(duì)返回值的影響詳解,執(zhí)行完try catch里面內(nèi)容準(zhǔn)備return時(shí),如果還有finally需要執(zhí)行這是編譯器會(huì)為我們?cè)黾右粋€(gè)全局變量去暫存return 的值,等到finally執(zhí)行完成去return這個(gè)全局變量,需要的朋友可以參考下
    2024-01-01
  • Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼

    Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼

    最近在做一個(gè)小項(xiàng)目,項(xiàng)目主要需求是實(shí)現(xiàn)一個(gè)文件上傳器,通過(guò)客戶端的登陸,把本地文件上傳到服務(wù)器的數(shù)據(jù)庫(kù)(本地的)。下面通過(guò)本文給大家分享下實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧
    2016-10-10
  • Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊

    Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊

    這篇文章主要介紹了Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Spring boot 集成 Druid 數(shù)據(jù)源過(guò)程詳解

    Spring boot 集成 Druid 數(shù)據(jù)源過(guò)程詳解

    這篇文章主要介紹了Spring boot 集成 Druid 數(shù)據(jù)源過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • SpringBoot中的多個(gè)事務(wù)管理詳解

    SpringBoot中的多個(gè)事務(wù)管理詳解

    這篇文章主要介紹了SpringBoot中的多個(gè)事務(wù)管理詳解,事務(wù)管理是一種組織和協(xié)調(diào)各種活動(dòng)和資源的方法,以實(shí)現(xiàn)特定目標(biāo),它涉及規(guī)劃、執(zhí)行和監(jiān)控各種任務(wù),以確保項(xiàng)目或組織的順利運(yùn)行,需要的朋友可以參考下
    2023-10-10
  • HttpsURLConnection上傳文件流(實(shí)例講解)

    HttpsURLConnection上傳文件流(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇HttpsURLConnection上傳文件流(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • java maven中如何引入自己的lib

    java maven中如何引入自己的lib

    在JavaMaven項(xiàng)目中引入自己的庫(kù)可以簡(jiǎn)化為幾個(gè)步驟:首先,確保庫(kù)以JAR格式存在或打包成JAR;其次,將JAR文件放置在項(xiàng)目目錄或安裝到本地Maven倉(cāng)庫(kù);最后,在pom.xml中添加依賴,這樣做可以使項(xiàng)目更加模塊化,便于管理和維護(hù),感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • SpringBoot啟動(dòng)之SpringApplication初始化詳解

    SpringBoot啟動(dòng)之SpringApplication初始化詳解

    這篇文章主要介紹了SpringBoot啟動(dòng)之SpringApplication初始化詳解,首先初始化資源加載器,默認(rèn)為null;斷言判斷主要資源類(lèi)不能為null,否則報(bào)錯(cuò),需要的朋友可以參考下
    2024-01-01
  • Spring?AOP簡(jiǎn)介及統(tǒng)一處理

    Spring?AOP簡(jiǎn)介及統(tǒng)一處理

    AOP面向切面編程,它是一種思想,它是對(duì)某一類(lèi)事情的集中處理,本文給大家介紹Spring?AOP簡(jiǎn)介及統(tǒng)一處理,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • 深入了解Java中的過(guò)濾器Filter和監(jiān)聽(tīng)器Listener

    深入了解Java中的過(guò)濾器Filter和監(jiān)聽(tīng)器Listener

    這篇文章主要為大家詳細(xì)介紹了Java中的過(guò)濾器Filter和監(jiān)聽(tīng)器Listener的使用以及二者的區(qū)別,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-06-06

最新評(píng)論

清新县| 宁阳县| 甘谷县| 历史| 梁河县| 平江县| 闻喜县| 昌平区| 南开区| 海门市| 永善县| 突泉县| 丰都县| 灯塔市| 楚雄市| 嵊州市| 务川| 泰宁县| 类乌齐县| 卢氏县| 平远县| 泰顺县| 景泰县| 江西省| 靖安县| 自贡市| 定边县| 浑源县| 连平县| 兴山县| 上蔡县| 吉木乃县| 德格县| 郁南县| 托克逊县| 凤城市| 广西| 方山县| 新密市| 兴隆县| 江陵县|