Spring Boot 2.3.x 升级到2.7.x 问题和解决

Spring Boot 2.3.x 升级到2.7.x 问题和解决

升级环境说明

目前项目使用的2.3.7版本(自己感觉还行,但是官方已经停止支持了。)

Spring Boot 官方支持情况
spring boot 官方支持情况

官方在今年8月就终止了对2.3.x的版本支持。2.7.x还有点时间。为啥不升级到3.0.x?太新,不适上线项目使用。

 

  • 当前Spring Boot 版本 2.3.7
  • 升级Spring Boot 版本 2.7.6
  • jdk 版本 1.8

一  thymeleaf-layout-dialect 版本

在Spring Boot 2.6.x以后版本需要使用3.0.0,同时暂时不能使用3.1.0。2.6.x后Spring Boot有对改组件的版本管理,所以配置添加配置的时候不需要配置版本即可

<dependency>
   <groupId>nz.net.ultraq.thymeleaf</groupId>
   <artifactId>thymeleaf-layout-dialect</artifactId>
   <!-- <version>2.4.1</version>-->
</dependency>

二 全局循环依赖解决配置

错误:


The dependencies of some of the beans in the application context form a cycle:

解决

spring
  main:
    allow-circular-references: true #自动解决循环依赖问题

三 资源文件映射配置变化

之前是:

spring
  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

升级到2.6.x 2.7.x 后

spring
  web:
    resources: #本地资源映射配置
      static-locations:  classpath:/static/,file:${app.file-root}/header #参考配置

四 mvc pathmatch 解决springfox swagger 初始化报错问题

spring boot 2.7.x修改了mvc默认的path匹配器,默认是

path_pattern_parser

修改为

ant_path_matcher

配置如下:

spring
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher #解决springfox swagger问题

五 ErrorController 接口去掉了getErrorPath方法

删除实现的getErrorPath()方法即可。

六 页面跳转错误

错误信息参考:

Cannot forward to error page for request [/a/] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false   org.springframework.boot.web.servlet.support.ErrorPageFilter.handleCommittedResponse(ErrorPageFilter.java:219) 

如果使用的不是
中的路径匹配器,是默认的,需要注意 以下写法对应不一样

  • /a
  • /a/

以上对于新的匹配器来说是两个不同的路径。

 

六 跨域配置问题

问题:

Caused by: java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

上面是因为我使用了@CrossOrigin(allowCredentials = “true”)注解。

解决办法是:

设置注解中的originPatterns为* ,不使用默认的 allowedOrigins *

 @CrossOrigin(allowCredentials = "true",originPatterns = "*")

如果你是代码全局配置的,则修改下面注释掉的地方即可

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*") //修改为下面的
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

 

Spring Boot 2.3.x 升级到2.7.x 问题和解决

相关文章:

你感兴趣的文章:

标签云: