SpringBoot整合SpringMVC之整合Fastjson

這篇文章將整合Fastjson作爲默認的JSON工具類,提高JSON的序列化與反序列化性能,用內置的FastJsonHttpMessageConverter替代SpringMVC默認的HttpMessageConverter


由於這一篇比較簡單所有前言也放在上面一起說了,下面直接講實現

1.增加依賴

<!-- 引入 Fastjson ,實現對 JSON 的序列化 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

2.修改SpringMVC配置類

//配置Fastjson
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //創建 fastJsonHttpMessageConverter 對象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //自定義FastJson配置
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.defaultCharset()); //設置字符集
        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); //剔除循環引用
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //設置支持的MediaType
        fastJsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.APPLICATION_JSON_UTF8));
        //添加到converters中
        converters.add(0, fastJsonHttpMessageConverter); //添加在最開頭
    }

謝謝觀看

原文鏈接:http://www.iocoder.cn/Spring-Boot/SpringMVC/?self

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章