阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

spring boot2 修改默认json解析器Jackson为fastjson

31次阅读
没有评论

共计 2834 个字符,预计需要花费 8 分钟才能阅读完成。

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

spring boot2 修改默认 json 解析器 Jackson 为 fastjson

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

        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>fastjsonlt;/artifactid>
            <version>1.2.62lt;/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> 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 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> 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 fastMediaTypes = new ArrayList();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        // 在 convert 中添加配置信息.
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(0,fastConverter);
    }
}

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2834字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中