Spring Boot 项目 WebFlux 与 Netty 兼容问题:Correct the classpath of your application so that it contains...

问题与处理策略

问题描述
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.8</version>
    <relativePath/>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
  • 在 Spring Boot 项目中,使用 WebFlux 时,报如下错误
***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    reactor.netty.transport.logging.ReactorNettyLoggingHandler.<init>(ReactorNettyLoggingHandler.java:65)

The following method did not exist:

    io.netty.handler.logging.LoggingHandler.<init>(Ljava/lang/String;Lio/netty/handler/logging/LogLevel;Lio/netty/handler/logging/ByteBufFormat;)V

The calling method's class, reactor.netty.transport.logging.ReactorNettyLoggingHandler, was loaded from the following location:

    jar:file:/D:/Maven/repository/io/projectreactor/netty/reactor-netty-core/1.0.19/reactor-netty-core-1.0.19.jar!/reactor/netty/transport/logging/ReactorNettyLoggingHandler.class

The called method's class, io.netty.handler.logging.LoggingHandler, is available from the following locations:

    jar:file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar!/io/netty/handler/logging/LoggingHandler.class
    jar:file:/D:/Maven/repository/io/netty/netty-handler/4.1.77.Final/netty-handler-4.1.77.Final.jar!/io/netty/handler/logging/LoggingHandler.class

The called method's class hierarchy was loaded from the following locations:

    io.netty.handler.logging.LoggingHandler: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
    io.netty.channel.ChannelDuplexHandler: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
    io.netty.channel.ChannelInboundHandlerAdapter: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
    io.netty.channel.ChannelHandlerAdapter: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar


Action:

Correct the classpath of your application so that it contains compatible versions of the classes reactor.netty.transport.logging.ReactorNettyLoggingHandler and io.netty.handler.logging.LoggingHandler
# 翻译

***************************
应用程序启动失败
***************************

描述:

尝试调用一个不存在的方法,调用发生在以下位置:

	reactor.netty.transport.logging.ReactorNettyLoggingHandler.<init>(ReactorNettyLoggingHandler.java:65)

以下方法不存在:

    io.netty.handler.logging.LoggingHandler.<init>(Ljava/lang/String;Lio/netty/handler/logging/LogLevel;Lio/netty/handler/logging/ByteBufFormat;)V

调用方的类(reactor.netty.transport.logging.ReactorNettyLoggingHandler)从以下位置加载:

	jar:file:/D:/Maven/repository/io/projectreactor/netty/reactor-netty-core/1.0.19/reactor-netty-core-1.0.19.jar!/reactor/netty/transport/logging/ReactorNettyLoggingHandler.class

被调用方的类继承关系从以下位置加载:

	io.netty.handler.logging.LoggingHandler: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
	io.netty.channel.ChannelDuplexHandler: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
	io.netty.channel.ChannelInboundHandlerAdapter: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar
	io.netty.channel.ChannelHandlerAdapter: file:/D:/Maven/repository/io/netty/netty-all/4.1.39.Final/netty-all-4.1.39.Final.jar


操作:

修正应用程序的类路径,确保包含兼容版本的以下类:
- reactor.netty.transport.logging.ReactorNettyLoggingHandler
- io.netty.handler.logging.LoggingHandler
问题原因
  1. Spring Boot 中的 WebFlux,默认依赖 Reactor Netty 作为底层 HTTP 服务器和客户端实现
  1. ReactorNettyLoggingHandler(来自 reactor-netty-core:1.0.19)尝试调用 LoggingHandler(来自 netty-all:4.1.39)的一个构造方法

  2. 但是,项目中混入了 不兼容的 Netty 版本,该构造方法在当前的 Netty 版本中不存在,因此报错

处理策略
  • 完全移除 Netty,Spring Boot 已自动管理 Netty 依赖,无需单独声明,这样可以使用统一的 Netty 版本
<!-- 移除这段配置 -->

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.39.Final</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值