Fastjson is produced by Ali. Although fasjson has exposed several serious vulnerabilities in recent years, in all fairness, the performance of fastjson is indeed very advantageous, especially when the amount of data is large, so fastjson is still our first choice; spring boot defaults The json parser is Jackson, and it is necessary to replace it with fastjson. |
1. Replacement method
1.1. Introduce dependencies, [Attention, there are serious high-risk vulnerabilities below 1.2.61, 1.2.61 repairs, must be upgraded to 1.2.61, the latest version is 1.2.62]
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjsonlt;/artifactid> <version>1.2.62lt;/version> </dependency>
1.2, configuration
Note: After Springboot 2.0, WebMvcConfigurerAdapter is obsolete. The method of inheriting WebMvcConfigurerAdapter from the previous version 1 is not recommended. Two configuration methods are introduced below. There is another method to implement WebMvcConfigurationSupport. There are tens of thousands of roads, and one option is enough:
Method one (recommended): replace the default parser with 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 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); * @Description Configure the message converter * @Date: 2019-12-8 11:23:33 * @version: 1.0 * new HttpMessageConverters(true, converters); * Must be set to true to replace otherwise it will not be replaced * @return Return a message conversion bean */ @Bean public HttpMessageConverters fastJsonMessageConverters() { List> converters = new ArrayList<>(); //You need to define an object that converts the message; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //Add fastJson Configuration information; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); //Global time configuration fastJsonConfig.setCharset(Charset.forName("UTF-8")); //Handle Chinese garbled characters List fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //Add configuration information in convert. fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(0, fastConverter); return new HttpMessageConverters(converters); } }
Method two, implement WebMvcConfigurer
@Configuration public class WebConfigure implements WebMvcConfigurer{ /** * Configure message converters * @param converters */ @Override public void configureMessageConverters(List> converters) { //You need to define an object that converts messages; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter( ); //Add fastJson configuration information; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //Global time configuration fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); fastJsonConfig.setCharset(Charset.forName("UTF-8")); //Handle Chinese garbled List fastMediaTypes = new ArrayList<>(); characters fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //Add configuration information in convert. fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(0,fastConverter); } )