1. 程式人生 > >SpringBoot(十):SpringBoot使用FastJson

SpringBoot(十):SpringBoot使用FastJson

阿里巴巴FastJson是一個Json處理工具包,包括“序列化”和“反序列化”兩部分,它具備如下特徵:
速度最快,測試表明,fastjson具有極快的效能,超越任其他的Java Json parser。包括自稱最快的JackJson;
功能強大,完全支援Java Bean、集合、Map、日期、Enum,支援範型,支援自省;無依賴,能夠直接執行在Java SE 5.0以上版本;支援Android;開源 (Apache 2.0)

另外一點,SpringBoot預設json轉換器為Jackson

一、依賴

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

二、修改預設json轉換器

package cn.aduu.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import
java.util.ArrayList; import java.util.List; /** * @author zh * @ClassName cn.aduu.config.FastJsonConfiguration * @Description */ @Configuration public class FastJsonConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //處理中文亂碼問題 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); } }

後續就可以使用Fastjson來轉換JSON了