1. 程式人生 > >SpringBoot 添加fastjson

SpringBoot 添加fastjson

處理 http oid ray format port add bean cti

1.先在項目中添加fastjson依賴:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>${fastjson.version}</version>
</dependency>

2.1第一種方法,讓入口類實現WebMvcConfigure接口,並重寫configureMessageConverters方法:

public class App implements WebMvcConfigurer{
     ......  
}
重寫方法:
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		//第一種配置fastJson方法
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		//處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(fastJsonHttpMessageConverter);
		
}

2.2第二種方法,直接在入口類加入@Bean註解並加入方法:

@Bean
public HttpMessageConverters fastJsonHttpMessage() {
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		//處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		return new HttpMessageConverters(fastJsonHttpMessageConverter);
}

  

SpringBoot 添加fastjson