原 Spring 项目转到 Spring boot 项目的一些变动

来源:互联网 发布:小影怎么添加网络音乐 编辑:程序博客网 时间:2024/05/19 15:41

只针对自己的项目,记录一下,改成 Spring boot 项目后,一些模块有了新的解决方案

characterEncodingFilter:

Spring boot 默认使用 utf-8 可省略该项配置 (spring.http.encoding.charset=utf-8)
JSON:
@ResponseBody 默认使用 jackson 转换 json 数据 ( 替换原 fastjson )
原项目使用 responseJsonStatus 返回 json 格式的字符串  ( Content-Type:text/html )
Spring boot 自动配置 MappingJackson2HttpMessageConverter 转换 json ( Content-Type:application/json )
原项目 $ajax 方法中需去掉  jQuery.parseJSON ( 接收参数已是 js 对象 )
easyui form.submit 请求数据类型为 html 所以接收的参数是 json 结构的字符串 , 所以需 jQuery.parseJSON 转换为 js 对象
@JsonIgnore 替换原 @JSONField(serialize = false)
jackson 遇到懒加载提示 FAIL_ON_EMPTY_BEANS, 解决:spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
Druid Datasource:
参考 https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
日志:

使用 Spring boot 默认的 Logback 替换原 lo4j2 , 默认配置只输出到控制台, 自定义配置使用 logback-spring.xml 文件

事务配置:
使用 JavaConfig 加载 xml 的方式,配置切片事务,@ImportResource(locations = { "classpath:transaction.xml" })
缓存配置:
Gradle 加入 spring-boot-starter-cache 依赖
启动类中加入 @EnableCaching 注解
服务层获取数据方法上使用 @Cacheable
Spring boot 默认使用 ConcurrentMapCacheManager 作为 CacheManager 的实现
使用 ehcache:
Gradle 中加入 ehcache.jar 依赖与 ehcache.xml
Spring boot 将自动装配 EhCacheCacheManager 作为 CacheManager 的实现
外部资源与文件上传:
Spring boot 如用 jar 包方式,上传目录将在 jar 外部,可通过 addResourceHandlers 增加外部资源的访问
属性格式化(字符转 Date.class):
Spring3 增加了一个 Converter 、Formatter 等接口,提供和 PropertyEditor 类似的功能
DateFormatter 替代原 BindingInitializer
启用 Spring Security:
注意:认证或授权失败需要考虑 普通和 Ajax 请求两种情况的处理,如果开启 csrf 同样需要考虑两种情况下的失败逻辑的处理
csrf 通过 servlet 抛出 403 ,并不能在 controller 层进行捕获与处理
比较简单的做法是通过 ajax error 函数处理 ( csrf 默认只保护 post 方法 )
默认开启 csrf 防护,Post 提交需要加上 csrf token 
默认 X-Frame-Options 是 DENY ,使用 kindeditor (可视化界面如上传等,是以 frame 提交) 需要注意设置成 sameOrigin , easyui 可以通过设置提交方式为:iframe:false 
去掉模板功能:
作用不大,以后项目后端只提供数据访问接口,终端处理界面的分配(且响应式布局流行)