栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

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

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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+

启动类报错

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

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配置方式的 @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下:


image.png


由此可以看出一些端倪,那就是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既可


    org.springframework.boot
    spring-boot-starter-web
Thymeleaf 3.0 默认不包含布局模块

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


   nz.net.ultraq.thymeleaf
   thymeleaf-layout-dialect
拦截器过时

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

public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter

新:

public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer
静态资源被拦截

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

 @Override public void addInterceptors(InterceptorRegistry registry) { 

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

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

image.png

全局异常特殊处理

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

@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"));
                }
            };
        }
    }



作者:嘟爷MD
链接:https://www.jianshu.com/p/3494c84b4be3


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号