Spring boot升级到2.3.1笔记

前言

老版本的Spring boot已经继续用了2年了,最近发现官网的推荐版本已经没有了1.5系列的了,下决心要升级大版本,把之前写的升级方案找出来参考了一下,这次应该还会碰到一些其他问题,特此记录以备不时之需

  • Spring boot版本:1.5.8.RELEASE -> 2.3.1.RELEASE
  • Spring Data JPA版本: 1.11.8.RELEASE -> 2.3.1.RELEASE

本来想按官网的更稳妥的方案一步一步升级,但是由于我们使用的Spring boot功能比较少,CTO建议直接升级到最新版本
先按两年前的方案修改各种配置:Spring boot升级到2.1

添加升级工具

升级文档中发现有升级工具,添加到pom中

1
2
3
4
5
6
<!-- 升级工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>

本地h2数据库保留字问题

查询了一下,h2数据库版本从1.4.1961.4.200,原先数据库表中有一些名称为row的字段,这些字段都变成保留字了,初始化的sql执行报错
原先思路是修改字段名称,这样改动太大了,CTO建议h2单独切换回老版本,测试后没有问题了

1
2
3
4
5
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version><!-- 添加一行指定版本号 -->
</dependency>

swagger2版本冲突问题

启动仍然报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 3 were found:
- relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
- linkDiscovererRegistry: defined in null
- entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Disconnected from the target VM, address: '127.0.0.1:61337', transport: 'socket'

Process finished with exit code 1

网上搜索了一下这个报错,说是和swagger2版本有关,修改pom中的版本号(2.2.2->2.9.2)之后问题就解决了

时间格式问题

测试类报了时间比对错误,去官网升级指南中找到了相关说明:
In 2.0, we’ve flipped a Jackson configuration default to write JSR-310 dates as ISO-8601 strings. If you wish to return to the previous behavior, you can add spring.jackson.serialization.write-dates-as-timestamps=true to your configuration.
在测试类中添加时间格式设置

1
spring.jackson.serialization.write-dates-as-timestamps = true

到此为止,新版本的兼容已经OK了,项目能正常运行了,测试类也全部可以通过了。
后面仍需要完善一些警告,过时方法等,经过小范围的试用后就可以确定升级方案了。

参考资料

Spring boot v1.5 → v2.0 升级指南:Spring Boot 2.0 Migration Guide
Spring boot 最新文档:Spring Boot Reference Documentation

文章目录
  1. 1. 前言
    1. 1.1. 添加升级工具
    2. 1.2. 本地h2数据库保留字问题
    3. 1.3. swagger2版本冲突问题
    4. 1.4. 时间格式问题
    5. 1.5. 参考资料