1 FastJson配置
1.1 FastJson基础知识
1.2 SpringBoot整合FastJson
1.2.1 导入FastJson依赖
<!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency>
1.2.2 配置FastJson
技巧01:配置类必须位于启动方法同一级别或者下面的级别
技巧02:WebMvcConfigurerAdapter已经过时
参考文档:点击前往
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 王杨帅 * @create 2018-05-09 14:33 * @desc FastJson配置 **/ @Configuration public class FastJsonConfiguration extends WebMvcConfigurationSupport { @Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // 01 调用父类的配置 super.configureMessageConverters(converters); // 02 实例化FastJson转换器 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // 03 数据类型配置 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 创建FastJson配置类 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( // 修改FastJson过滤配置 SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty ); // 05 为FastJson转换器设置FastJson配置类 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // 06 将FastJson转换器添加到视图消息转换器列表中 converters.add(fastJsonHttpMessageConverter); } }
1.3 坑01
1.3.1 错误信息
Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'
1.3.2 解决办法
2 Druid配置
2.1 导入druid依赖
<!--druid数据库连接池--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
2.2 数据源配置
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 #最大活跃数 maxActive: 20 #初始化数量 initialSize: 1 #最大连接等待超时时间 maxWait: 60000 #打开PSCache,并且指定每个连接PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 #通过connectionProperties属性来打开mergeSql功能;慢SQL记录 #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 #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙 filters: stat, wall, log4j jpa: properties: hibernate: show_sql: true format_sql: true
2.3 Druid配置类
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 王杨帅 * @create 2018-05-09 11:31 * @desc 数据库监控配置 **/ @Configuration public class DruidConfigruration { @Bean public ServletRegistrationBean statViewServlet(){ //创建servlet注册实体 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //设置ip白名单 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow servletRegistrationBean.addInitParameter("deny","192.168.0.19"); //设置控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername","druid"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否可以重置数据 servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ //创建过滤器 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //设置过滤器过滤路径 filterRegistrationBean.addUrlPatterns("/*"); //忽略过滤的形式 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }