升级SpringBoot3.x启动报错汇总

项目背景:

springboot 2.x 升级到springboot 3.x


问题1

启动报错:

Correct the classpath of your application so that it contains compatible versions of the classes org.redisson.connection.ServiceManager and io.netty.channel.nio.NioEventLoopGroup

原因分析

本地netty依赖

  <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>5.0.0.Alpha2</version>
  </dependency>

netty的版本过高

解决方案:

在报错信息中找到redisson对应的netty版本,然后将netty版本降至该版本。如果不清楚redisson对应的版本,可用以下命令找到依赖关系

 mvn dependency:tree
[INFO] +- com.alipay.sofa:rpc-sofa-boot-starter:jar:4.1.0:compile
[INFO] |  +- com.alipay.sofa:sofa-boot-starter:jar:4.1.0:compile
[INFO] |  |  +- com.alipay.sofa:sofa-boot:jar:4.1.0:compile
[INFO] |  |  |  \- com.alipay.sofa.common:sofa-common-tools:jar:2.0.1:compile
[INFO] |  |  \- com.alipay.sofa:sofa-boot-autoconfigure:jar:4.1.0:compile
[INFO] |  \- com.alipay.sofa:rpc-sofa-boot:jar:4.1.0:compile
[INFO] |     \- com.alipay.sofa:sofa-rpc-all:jar:5.11.0:compile
[INFO] |        +- com.alipay.sofa:bolt:jar:1.6.6:compile
[INFO] |        +- org.javassist:javassist:jar:3.29.2-GA:compile
[INFO] |        +- io.netty:netty-all:jar:4.1.100.Final:compile
[INFO] |        |  +- io.netty:netty-buffer:jar:4.1.100.Final:compile

我本地的解决方案是将netty版本降至4.1.100.Final


问题2

启动报错:

required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

原因分析

未注入AuthenticationManager的Bean

解决方案:

在配置类中加入以下代码

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
    return config.getAuthenticationManager();
}

问题3

启动报错:

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

原因分析

springboot 从2.6开始默认不允许Bean的循环引用,也就是在Bean定义的类上不允许出现循环引用!

解决方案:

最优解:优化代码,去掉循环依赖

退而求其次,在配置文件中加入以下配置,保证启动不报错

spring.main.allow-circular-references=true

问题4

启动报错:

Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly

原因分析

springboot 从2.4开始不再自动注册嵌入式servlet容器DefaultServlet

解决方案:

在配置文件中加入以下配置

server:
  servlet:
    register-default-servlet: true

问题5

启动报错:

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.

原因分析

本地代码

/**
 * 添加跨域设置
 * 如果是react等框架带有options预请求的方法的,要放行options的支持
 */
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
    		.allowedMethods("POST", "GET", "OPTIONS", "DELETE")  
            .allowedOrigins("*")
            .allowCredentials(true);
}

springboot2.4.0以上对AllowedOrigin设置发生了改变,不能有 *

解决方案:

将上面的代码改成以下代码(allowedOrigins → allowedOriginPatterns

/**
 * 添加跨域设置
 * 如果是react等框架带有options预请求的方法的,要放行options的支持
 */
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
    		.allowedMethods("POST", "GET", "OPTIONS", "DELETE")  
            .allowedOriginPatterns("*")
            .allowCredentials(true);
}

问题6

启动报错:

Property 'spring.profiles' imported from location 'class path resource [application-dev.yml]' is invalid and should be replaced with 'spring.config.activate.on-profile' [origin: class path resource [application-dev.yml] - 19:13]

原因分析

springboot2.4版本配置更新

正确的配置:

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

spring:
  config:
    activate:
      on-profile: 
      - dev

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值