1 FastJson configuration
1.1 Basic knowledge of FastJson
1.2 SpringBoot integrates FastJson
1.2.1 Import FastJson dependencies
<!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency>
1.2.2 Configure FastJson
Tip 01: The configuration class must be at the same level or below the startup method
Trick 02: WebMvcConfigurerAdapter is outdated
Reference documents: click here
package cn.test.demo.base_demo.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.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.ArrayList; import java.util.List; /** * @author Wang Yangshuai * @create 2018-05-09 14:33 * @desc FastJson configuration **/ @Configuration public class FastJsonConfiguration extends WebMvcConfigurationSupport { @Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // 01 call the super class configuration super .configureMessageConverters(converters); // 02 instantiate the FastJson converter FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // 03 Data type configuration List<MediaType> supportedMediaTypes = new ArrayList<> (); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); supportedMediaTypes.add(MediaType.APPLICATION_PDF); supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); supportedMediaTypes.add(MediaType.APPLICATION_XML); supportedMediaTypes.add(MediaType.IMAGE_GIF); supportedMediaTypes.add(MediaType.IMAGE_JPEG); supportedMediaTypes.add(MediaType.IMAGE_PNG); supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); supportedMediaTypes.add(MediaType.TEXT_HTML); supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); supportedMediaTypes.add(MediaType.TEXT_PLAIN); supportedMediaTypes.add(MediaType.TEXT_XML); fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes); // 04 Create FastJson configuration class FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( // Modify FastJson filter configuration SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty ); // 05 Set the FastJson configuration class for the FastJson converter fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // 06 Add the FastJson converter to the list of view message converters converters.add(fastJsonHttpMessageConverter); } }
1.3 Pit 01
1.3.1 Error messages
Spring Boot configures FastJson to report an error 'Content-Type' cannot contain wildcard type '*'
1.3.2 Solution
2 Druid configuration
2.1 Import druid dependencies
<!--druid数据库连接池--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
2.2 Data source configuration
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/testdemo?useUnicode=true&characterEncoding=UTF-8&&useSSL=false driver-class-name: com.mysql.jdbc.Driver username: root password: root type: com.alibaba.druid.pool.DruidDataSource #Maximum active count maxActive: 20 #initialize the number initialSize: 1 #Maximum connection wait timeout maxWait: 60000 #Open PSCache and specify the size of each connection PSCache poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 #Open the mergeSql function through the connectionProperties property; slow SQL records #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 1 from dual testWhileIdle: true testOnBorrow: false testOnReturn: false #Configure the filters for monitoring statistics interception, after removing the monitoring interface sql will not be able to count, 'wall' is used for firewalls filters: stat, wall, log4j jpa: properties: hibernate: show_sql: true format_sql: true
2.3 Druid configuration class
package cn.test.demo.base_demo.config; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Wang Yangshuai * @create 2018-05-09 11:31 * @desc database monitoring configuration **/ @Configuration public class DruidConfigruration { @Bean public ServletRegistrationBean statViewServlet(){ // Create servlet registration entity ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean( new StatViewServlet(),"/druid/*" ); // Set ip whitelist servletRegistrationBean.addInitParameter("allow","127.0.0.1" ); // Set ip blacklist, if allow and deny coexist, deny takes precedence over allow servletRegistrationBean.addInitParameter("deny","192.168.0.19" ); // Set console management user servletRegistrationBean.addInitParameter("loginUsername" ,"druid" ); servletRegistrationBean.addInitParameter( "loginPassword","123456" ); // Whether the data can be reset servletRegistrationBean.addInitParameter("resetEnable","false" ); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ // Create a filter FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( new WebStatFilter()); // Set the filter path filterRegistrationBean.addUrlPatterns("/*" ); // Ignore the form of filtering filterRegistrationBean.addInitParameter( "exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" ); return filterRegistrationBean; } }