使用MQTT协议实现订阅与发布时遇到的坑-Jar包冲突

在使用MQTT协议实现订阅与发布时,遇到了Bean初始化失败的NoSuchMethodError,经排查发现是由于spring核心包版本不一致导致的jar包冲突。通过升级spring-integration-core版本至4.3.8-RELEASE,使其与Maven库中的spring版本一致,并排除其他包中重复的spring依赖,最终解决了问题。更新Maven库并重启项目后,问题成功解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误日志:

Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Z

引入的依赖包:

<dependency> 

    <groupId>org.springframework.integration</groupId>

    <artifactId>spring-integration-core</artifactId>

    <version>4.2.7.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.springframework.integration</groupId>

    <artifactId>spring-integration-mqtt</artifactId>

    <version>4.2.7.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.eclipse.paho</groupId>

    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>

    <version>1.2.0</version>

</dependency>

<dependency>

    <groupId>de.odysseus.staxon</groupId>

    <artifactId>staxon</artifactId>

    <version>1.2</version>

</dependency>

根据其他码友的说法,遇到

 Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class

这个错误,很大概率是因为jar包冲突导致的。然后我就去排查jar包的版本。

 maven库中的spring依赖包版本:

要引入的spring-integration-core包中的spring依赖包版本:

 发现Maven库中的Spring-core、spring-aop等jar包的版本是4.3.7-RELEASE,与要引入的spring-integration-core包中的spring依赖包版本4.2.6-RELEASE不一致。

要解决问题,就要使spring-integration-core包中的依赖包版本与maven库中的spring依赖包版本一致。

而在引包的过程中,我们发现spring-integration-core包中的依赖包的版本是spring-integration-core包版本4.2.7-RELEASE的上一版本,即4.2.6-RELEASE。

所以,我们只需要将spring-integration-core的版本号修改为4.3.8-RELEASE。那其中的依赖包版本即为4.3.7-RELEASE,也就与maven库中的spring依赖包版本一致。

再排除掉spring-integration-core包中的spring依赖包

 而后,我们又发现,要引入的第二个包spring-integration-mqtt包中已经引入了第一个包spring-integration-core,

 

 所以我们只需要引入spring-integration-mqtt包,再排除掉其中的spring依赖包即可。

 更新Maven库,重启项目,成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值