项目背景:
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