Spring Boot 2.0干货系列:(一)Spring Boot1.5X升级到2.0指南

Spring Boot 2.0干货系列:(一)Spring Boot1.5X升级到2.0指南

前言

Spring Boot已经发布2.0有4个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把本博客中Spring Boot干货系列对应的源码从1.5X升级到Spring Boot 2.0,顺便整理下升级的时候遇到的一些坑,做个记录。后续的教程就以最新的2.03版本为主。

正文

闲话不多说,本篇基于上一篇的源码chapter13为参考,直接升级到2.0.3.RELEASE,然后看着一堆报错开搞。在修改的过程中记录下来,方便后续升级的朋友少踩一些坑。

依赖 JDK 版本升级

2.x 至少需要 JDK 8 的支持,2.x 里面的许多方法应用了 JDK 8 的许多高级新特性,所以你要升级到 2.0 版本,先确认你的应用必须兼容 JDK 8。

另外,2.x 开始了对 JDK 9 的支持。

第三方类库升级

2.x 对第三方类库升级了所有能升级的稳定版本,一些值得关注的类库升级我给列出来了。

1) Spring Framework 5+
2) Tomcat 8.5+
3) Flyway 5+
4) Hibernate 5.2+
5) Thymeleaf 3+

相关更多依赖版本参考:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

启动类报错

问题:
启动类SpringBootServletInitializer标红报错,导入的类不对。
原因:
Spring Boot 部署到 Tomcat 中去启动时需要在启动类添加SpringBootServletInitializer,2.0 和 1.0 有区别。
解决方案:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.dudu;

import com.dudu.util.MyMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;

@SpringBootApplication
//启注解事务管理
@EnableTransactionManagement  // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@MapperScan(basePackages = "com.dudu.dao", markerInterface = MyMapper.class)

public class Application  extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

配置文件报错

问题:
配置文件中项目名称配置报错:server.context-path: /spring
原因:
大量的Servlet专属的server.* properties被移到了server.servlet下:
Spring Boot 2.0干货系列:(一)Spring Boot1.5X升级到2.0指南
由此可以看出一些端倪,那就是server不再是只有servlet了,还有其他的要加入。

解决方案:
server.context-path: /spring改成server.servlet.context-path: /spring既可

Web Starter 作为传递依赖

问题:
工程用的模板是thymeleaf,启动报错提示找不到spring-boot-starter-web
原因:
以前有几个 Spring Boot starter 是依赖于 Spring MVC 而传递的spring-boot-starter-web。在 Spring WebFlux 新的支持下,spring-boot-starter-mustache,spring-boot-starter-freemarker并spring-boot-starter-thymeleaf不再依赖它。开发者有责任选择和添加spring-boot-starter-web或spring-boot-starter-webflux。
解决方案:
导入spring-boot-starter-web既可

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

Thymeleaf 3.0 默认不包含布局模块

问题:
启动项目的时候发现首页空白,查看后台也没有任何的报错信息
原因:
Spring Boot 2.0 中spring-boot-starter-thymeleaf 包默认并不包含布局模块,需要使用的时候单独添加。
解决方案:

1
2
3
4
<dependency>
   <groupId>nz.net.ultraq.thymeleaf</groupId>
   <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

 

拦截器过时

问题:
升级后,WebMvcConfigurerAdapter提示过时
原因:
升级后的springBoot,使用了java8的特性 default 方法,所以直接实现 WebMvcConfigurer 这个接口即可。
解决方案:
旧:

1
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter

 

新:

1
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer

 

静态资源被拦截

问题:
访问系统的时候登录样式没有加载
原因:
1.5版本时候META-INF/resources / resources / static / public 都是spring boot 认为静态资源应该放置的位置,会自动去寻找静态资源,而在spring boot 2.0则对静态资源也进行了拦截,当拦截器拦截到请求之后,但controller里并没有对应的请求时,该请求会被当成是对静态资源的请求。此时的handler就是 ResourceHttpRequestHandler,就会抛出上述错误。
解决方案:
解决办法就是,在拦截器那里排除静态资源的请求路径

1
2
3
4
5
6
7
8
9
10
11
/**  
* 拦截器 
* @param registry 
*/ 
@Override 
public void addInterceptors(InterceptorRegistry registry) { 

// addPathPatterns 用于添加拦截规则 
// excludePathPatterns 用户排除拦截 
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatternss("/toLogin","/login","/assets/**","/js/**");
}

assets就是我放静态文件的目录

Spring Boot 2.0干货系列:(一)Spring Boot1.5X升级到2.0指南

全局异常特殊处理

问题:
上一篇提到过的有些错误你可能想特殊对待处理的,现在对应代码标红,找不到对应的类
原因:
新版本后该方法去掉了,需要换成新的方法处理
解决方案:
旧代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
    public class ContainerConfig {
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer(){
            return new EmbeddedServletContainerCustomizer(){
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
                    container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
                }
            };
        }
    }

 

新代码:

1
2
3
4
5
6
7
8
9
10
@Configuration
public class ContainerConfig implements ErrorPageRegistrar {
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage[] errorPages = new ErrorPage[2];
        errorPages[0] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
        errorPages[1] = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
        registry.addErrorPages(errorPages);
    }
}

 

暂时处理了以上几个错误后,项目就可以启动了,还有其他隐藏的错误后续遇到了再补充。