fastjson是阿里出的,尽管近年fasjson爆出过几次严重漏洞,但是平心而论,fastjson的性能的确很有优势,尤其是大数据量时的性能优势,所以fastjson依然是我们的首选;spring boot默认的json解析器是jackson,替换为fastjson很有必要;
<!-- fastjson --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.62</version> </dependency>
注意:springboot2.0以后,webmvcconfigureradapter 过时了, 以前1版本继承webmvcconfigureradapter 来实现的方法不推荐了。下面介绍两种配置方式,还有一种实现webmvcconfigurationsupport的方式就不介绍了,道路千万条,选一条就足够了:
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); } }
如对本文有疑问, 点击进行留言回复!!
VideoStudio多相机编辑器怎么用? VideoStudio多相机的使用方法
网友评论