导读 | fastjson是阿里出的,尽管近年fasjson爆出过几次严重漏洞,但是平心而论,fastjson的性能的确很有优势,尤其是大数据量时的性能优势,所以fastjson依然是我们的首选;spring boot默认的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); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/125545.html