1 FastJson配置

  1.1 FastJson基础知识

    点击前往

  1.2 SpringBoot整合FastJson

    点击前往

    1.2.1 导入FastJson依赖

  1. <!--fastjson-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.46</version>
  6. </dependency>

    1.2.2 配置FastJson

      技巧01:配置类必须位于启动方法同一级别或者下面的级别

      技巧02:WebMvcConfigurerAdapter已经过时

        参考文档:点击前往

  1. package cn.test.demo.base_demo.config;
  2.  
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import com.alibaba.fastjson.support.config.FastJsonConfig;
  5. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.converter.HttpMessageConverter;
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  10. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  11. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. /**
  17. * @author 王杨帅
  18. * @create 2018-05-09 14:33
  19. * @desc FastJson配置
  20. **/
  21. @Configuration
  22. public class FastJsonConfiguration extends WebMvcConfigurationSupport {
  23.  
  24. @Override
  25. protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  26. // 01 调用父类的配置
  27. super.configureMessageConverters(converters);
  28. // 02 实例化FastJson转换器
  29. FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
  30.  
  31. // 03 数据类型配置
  32. List<MediaType> supportedMediaTypes = new ArrayList<>();
  33. supportedMediaTypes.add(MediaType.APPLICATION_JSON);
  34. supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  35. supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
  36. supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
  37. supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
  38. supportedMediaTypes.add(MediaType.APPLICATION_PDF);
  39. supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
  40. supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
  41. supportedMediaTypes.add(MediaType.APPLICATION_XML);
  42. supportedMediaTypes.add(MediaType.IMAGE_GIF);
  43. supportedMediaTypes.add(MediaType.IMAGE_JPEG);
  44. supportedMediaTypes.add(MediaType.IMAGE_PNG);
  45. supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
  46. supportedMediaTypes.add(MediaType.TEXT_HTML);
  47. supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
  48. supportedMediaTypes.add(MediaType.TEXT_PLAIN);
  49. supportedMediaTypes.add(MediaType.TEXT_XML);
  50. fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
  51.  
  52. // 04 创建FastJson配置类
  53. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  54. fastJsonConfig.setSerializerFeatures( // 修改FastJson过滤配置
  55. SerializerFeature.DisableCircularReferenceDetect,
  56. SerializerFeature.WriteMapNullValue,
  57. SerializerFeature.WriteNullStringAsEmpty
  58. );
  59.  
  60. // 05 为FastJson转换器设置FastJson配置类
  61. fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
  62.  
  63. // 06 将FastJson转换器添加到视图消息转换器列表中
  64. converters.add(fastJsonHttpMessageConverter);
  65.  
  66. }
  67. }

FastJsonConfiguration.java

  1.3 坑01

    1.3.1 错误信息

      Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'

    1.3.2 解决办法

      点击前往

2 Druid配置

  2.1 导入druid依赖

  1. <!--druid数据库连接池-->
  2. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  3. <dependency>
  4. <groupId>com.alibaba</groupId>
  5. <artifactId>druid</artifactId>
  6. <version>1.0.29</version>
  7. </dependency>

  2.2 数据源配置

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://127.0.0.1:3306/testdemo?useUnicode=true&characterEncoding=UTF-8&&useSSL=false
  4. driver-class-name: com.mysql.jdbc.Driver
  5. username: root
  6. password: root
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. #最大活跃数
  9. maxActive: 20
  10. #初始化数量
  11. initialSize: 1
  12. #最大连接等待超时时间
  13. maxWait: 60000
  14. #打开PSCache,并且指定每个连接PSCache的大小
  15. poolPreparedStatements: true
  16. maxPoolPreparedStatementPerConnectionSize: 20
  17. #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
  18. #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  19. minIdle: 1
  20. timeBetweenEvictionRunsMillis: 60000
  21. minEvictableIdleTimeMillis: 300000
  22. validationQuery: select 1 from dual
  23. testWhileIdle: true
  24. testOnBorrow: false
  25. testOnReturn: false
  26. #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
  27. filters: stat, wall, log4j
  28.  
  29. jpa:
  30. properties:
  31. hibernate:
  32. show_sql: true
  33. format_sql: true

  2.3 Druid配置类

  1. package cn.test.demo.base_demo.config;
  2.  
  3. import com.alibaba.druid.support.http.StatViewServlet;
  4. import com.alibaba.druid.support.http.WebStatFilter;
  5. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  6. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9.  
  10. /**
  11. * @author 王杨帅
  12. * @create 2018-05-09 11:31
  13. * @desc 数据库监控配置
  14. **/
  15. @Configuration
  16. public class DruidConfigruration {
  17. @Bean
  18. public ServletRegistrationBean statViewServlet(){
  19. //创建servlet注册实体
  20. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
  21. //设置ip白名单
  22. servletRegistrationBean.addInitParameter("allow","127.0.0.1");
  23. //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
  24. servletRegistrationBean.addInitParameter("deny","192.168.0.19");
  25. //设置控制台管理用户
  26. servletRegistrationBean.addInitParameter("loginUsername","druid");
  27. servletRegistrationBean.addInitParameter("loginPassword","123456");
  28. //是否可以重置数据
  29. servletRegistrationBean.addInitParameter("resetEnable","false");
  30. return servletRegistrationBean;
  31. }
  32.  
  33. @Bean
  34. public FilterRegistrationBean statFilter(){
  35. //创建过滤器
  36. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
  37. //设置过滤器过滤路径
  38. filterRegistrationBean.addUrlPatterns("/*");
  39. //忽略过滤的形式
  40. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  41. return filterRegistrationBean;
  42. }
  43.  
  44. }

DruidConfigruration.java

  2.4 参考博文

    点击前往

SpringBoot17 FastJson配置、Druid配置的更多相关文章

  1. 数据库连接池优化配置(druid,dbcp,c3p0)

    主要描述了数据库连接池参数配置的准则,针对常用的数据库连接池(c3p0,dbcp,druid)给出推荐的配置. 考虑因素 1:当前连接DB的规模   2:并发情况 3:执行db的响应时间 配置考虑 1 ...

  2. druid配置数据库连接使用密文密码

    spring使用druid配置dataSource片段代码 dataSource配置 <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="data ...

  3. [转]阿里巴巴数据库连接池 druid配置详解

    一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...

  4. JNDI学习总结(三)——Tomcat下使用Druid配置JNDI数据源

    com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...

  5. druid配置(转)

    java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,有不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色的性能,也 ...

  6. 最新 Druid 配置

    Druid是一个JDBC组件库,包括数据库连接池.SQL Parser等组件.DruidDataSource是最好的数据库连接池.下面我们就一起来在项目中配置Druid吧 1.Druid依赖配置 &l ...

  7. Tomcat下使用Druid配置JNDI数据源

    com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...

  8. JFinal Druid 配置

    /** * 数据库密码加密,执行如下命令,生成加密密码 * java -cp druid-1.1.14.jar com.alibaba.druid.filter.config.ConfigTools ...

  9. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

随机推荐

  1. tslib: Selected device is not a touchscreen (must support ABS_X and ABS_Y events)

    /************************************************************************************ * tslib: Selec ...

  2. POJ1797 Heavy Transportation

    解题思路:典型的Kruskal,不能用floyed(会超时),上代码: #include<cstdio> #include<cstring> #include<algor ...

  3. Linux下shell命令 1

    1   [root@hadoop-namenode-1 iebd] cd /filename/filename  跳转至filename文件夹 2   [root@hadoop-namenode-1 ...

  4. 圆方树总结 [uoj30]Tourists

    圆方树总结 所谓圆方树就是把一张图变成一棵树. 怎么变啊qaq 这里盗一张图 简单来说就是给每一个点双新建一个点,然后连向这个点双中的每一个点.特殊的,把两个点互相连通的也视作一个点双. 我们把原来就 ...

  5. 【3】SpringMVC的Controller

    1SpringMvc的Controller是线程安全的吗? (1)由于是单例,tomcat的多线程环境访问,属性必须是不可变的,如果可变,会产生脏数据,线程不安全 2Spring的事务管理 (1)ao ...

  6. Android 杂记

    Android Studio 报错:sdk location should not contain whitespace as this can cause problems with the ndk ...

  7. 谷歌设置支持webgl

    浏览器报错: could not initialize WebGl 因为谷歌默认不支持WebGl 在浏览器器中输入 about:flags 然后开启:覆盖软件渲染列表,覆盖内置的软件渲染列表,并对不支 ...

  8. 程序4-3 umask函数实例

    //http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...

  9. SQL Server数据库优化经验总结

    优化数据库的注意事项: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化.(可以用Sybase的SQL Expert, ...

  10. python's fifteenth day for me 递归函数

    递归... def age(n): if n == 1: return 18 else: return age(n-1)+2 # 反复调用函数age() print(age(4)) l = [1,3, ...