Spring boot升级到2.1

前言

用了Spring boot一段时间了,之前都是小版本升级,后来稳定在1.5.8,最近想研究一下升级大版本,于是开始升级之路,将遇到的问题和解决方案记录一下,以备不时之需

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

更新版本号 - pom.xml

在pom.xml中更换版本号,然后maven更新

1
2
3
4
5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>

入口 - SpringBootServletInitializer

1
2
3
4
5
// 更新前
import org.springframework.boot.web.support.SpringBootServletInitializer;

// 更新后
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

错误处理 - ErrorPageController

之前的ErrorPageController是一个错误处理的控制类,替换默认的控制类加入写日志的操作,更新版本后一些默认的类已经发生改变了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 以下3个对象已经更换到新的包中
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;

// 更新后
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;

// ErrorAttributes.getErrorAttributes方法参数从HttpServletRequest改为WebRequest
private ErrorAttributes errorAttributes = new DefaultErrorAttributes();
private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes, false);
}

// 更新后
private Map<String, Object> getErrorAttributes(WebRequest request) {
return this.errorAttributes.getErrorAttributes(request, false);
}

开启多个连接器(同时支持http和https) - MultiConnectorsConfiguration

有些小改动,照抄官网例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 更新前
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;

@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
// 更新后
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}

计数器 - CounterService

新版本没有这个类了,暂时先去掉

1
2
3
4
5
6
import org.springframework.boot.actuate.metrics.CounterService;

// codehale metrics
@Autowired
@Qualifier("counterService")
private CounterService counterService;

Spring Data JPA相关的问题

存储库的一些自带方法变化了

1
2
3
4
5
6
7
8
9
10
11
// 老的版本
Book book = dao.findOne(id);
dao.save(list);
dao.delete(id);
dao.delete(list);

// 新的版本
Optional<Book> book = dao.findById(id);
dao.saveAll(list);
dao.deleteById(id);
dao.deleteAll(list);

Spring Boot Admin相关的问题

Spring Boot Admin也需要使用新的版本了,否则无法启动,配置文件一些参数发生了变化
版本升级:1.5.4 -> 2.1.0

Spring Boot Admin的server端也需要更新,更新后的UI已经用Vue重写了,很惊艳!

配置文件改动:

1
2
3
4
5
6
7
8
9
# 老的版本
spring.boot.admin.url=...
spring.boot.admin.client.service-url=...
management.security.enabled=false

# 新的版本
spring.boot.admin.client.url=...
spring.boot.admin.client.instance.service-url=...
management.endpoints.web.exposure.include=*

其他配置改动 - application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
# 老的版本
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=20MB

spring.datasource.initialize=true
flyway.enabled=false

# 新的版本
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=20MB

spring.datasource.initialization-mode=always
spring.flyway.enabled=false

跨域问题 - CORSConfiguration

原先配置的WebMvcConfigurerAdapter已经过期了,现在直接用WebMvcConfigurer

1
2
3
4
// 老的版本
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
// 新的版本
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

未完待续。。。

经过修改,项目已经可以运行了,接下来开始解决测试类报错的问题

文章目录
  1. 1. 前言
    1. 1.1. 更新版本号 - pom.xml
    2. 1.2. 入口 - SpringBootServletInitializer
    3. 1.3. 错误处理 - ErrorPageController
    4. 1.4. 开启多个连接器(同时支持http和https) - MultiConnectorsConfiguration
    5. 1.5. 计数器 - CounterService
    6. 1.6. Spring Data JPA相关的问题
    7. 1.7. Spring Boot Admin相关的问题
    8. 1.8. 其他配置改动 - application.properties
    9. 1.9. 跨域问题 - CORSConfiguration
    10. 1.10. 未完待续。。。