zoukankan      html  css  js  c++  java
  • spring boot2 修改默认json解析器Jackson为fastjson

    0、前言

      fastjson是阿里出的,尽管近年fasjson爆出过几次严重漏洞,但是平心而论,fastjson的性能的确很有优势,尤其是大数据量时的性能优势,所以fastjson依然是我们的首选;spring boot默认的json解析器是Jackson,替换为fastjson很有必要;

    1、替换方法

    1.1、引入依赖,【注意,1.2.61以下有严重高危漏洞,1.2.61修复,必须升级到1.2.61,目前最新版本为1.2.62

            <!-- fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.62</version>
            </dependency>

     1.2、配置

         注意:Springboot2.0以后,WebMvcConfigurerAdapter 过时了, 以前1版本继承WebMvcConfigurerAdapter 来实现的方法不推荐了。下面介绍两种配置方式,还有一种实现WebMvcConfigurationSupport的方式就不介绍了,道路千万条,选一条就足够了:

    方式一(推荐):用bean替代默认解析器

    package com.anson.config;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    public class WebConfig {
    
        /**
         * @Author anson
         * @Description 配置消息转换器
         * @Date: 2019-12-8 11:23:33
         * @version: 1.0
         * new HttpMessageConverters(true, converters);
         * 一定要设为true才能替换否则不会替换
         * @return 返回一个消息转换的bean
         */
        @Bean
        public HttpMessageConverters fastJsonMessageConverters() {
            List<HttpMessageConverter<?>> converters = new ArrayList<>();
            //需要定义一个convert转换消息的对象;
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            //添加fastJson的配置信息;
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //全局时间配置
            fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
            fastJsonConfig.setCharset(Charset.forName("UTF-8"));
            //处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            //在convert中添加配置信息.
            fastConverter.setSupportedMediaTypes(fastMediaTypes);
            fastConverter.setFastJsonConfig(fastJsonConfig);
    
            converters.add(0, fastConverter);
            return new HttpMessageConverters(converters);
        }
    }

    方式二、实现WebMvcConfigurer

    @Configuration
    public class WebConfigure implements WebMvcConfigurer

    {
    /** * 配置消息转换器 * @param converters */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //需要定义一个convert转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加fastJson的配置信息; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //全局时间配置 fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); fastJsonConfig.setCharset(Charset.forName("UTF-8")); //处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //在convert中添加配置信息. fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(0,fastConverter); } }
  • 相关阅读:
    揭秘!阿里实时数仓分布式事务Scale Out设计
    ElementUI中使用el-time-picker向SpringBoot传输24小时制时间参数以及数据库中怎样存储
    Vue中实现清空数组和清空el-table
    Vue中foreach数组与js中遍历数组的写法
    Vue中向js中传递参数并在js中定义对象并转换参数
    若依管理系统导出Excel时添加没有的列和关联码表显示中文进行导出
    MyBatis中传递数组参数和List参数时if-test判空和判断长度的写法
    ElementUI中显示是否以及SpringBoot中怎样存储实体类属性和数据库怎样设计字段
    ElementUI中的el-form怎样格式化显示1和0为是和否
    Vue中通过Axios向SpringBoot发送get和post请求
  • 原文地址:https://www.cnblogs.com/yanghj/p/12005130.html
Copyright © 2011-2022 走看看