解决 An attempt was made to call a method that does not exist. 问题详解

哈喽大家好,我是阿Q。今天在开发代码的过程中,由于手抖,不知道引入了什么包依赖,导致项目启动一直报错,特写本文来记录下解决问题的经过。

有想赚点外块|技术交流的朋友,欢迎来撩

问题描述

报错信息如下

Description:

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

    io.undertow.websockets.jsr.Bootstrap.handleDeployment(Bootstrap.java:84)

The following method did not exist:

    javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

The method's class, javax.servlet.ServletContext, is available from the following locations:

    jar:file:/E:/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
    jar:file:/E:/.m2/repository/jakarta/servlet/jakarta.servlet-api/4.0.4/jakarta.servlet-api-4.0.4.jar!/javax/servlet/ServletContext.class

The class hierarchy was loaded from the following locations:

    javax.servlet.ServletContext: file:/E:/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext

报错描述

试图从以下位置去调用一个不存在的方法:io.undertow.websockets.jsr.Bootstrap.handleDeployment(Bootstrap.java:84);

javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;该方法不存在;

该方法的类javax.servlet.ServletContext可从以下位置获得:

 jar:file:/E:/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
 jar:file:/E:/.m2/repository/jakarta/servlet/jakarta.servlet-api/4.0.4/jakarta.servlet-api-4.0.4.jar!/javax/servlet/ServletContext.class

类层次结构是从以下位置加载的:javax.servlet.ServletContext: file:/E:/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar

更正应用程序的类路径,使其包含一个单独的兼容版本的javax.servlet.ServletContext

上边的解释说人话就是servlet-apijakarta.servlet-api下的 ServletContext.class 冲突了,需要修改一下版本依赖。

解决方法

首先打开IDEA的 setting 设置

在这里插入图片描述
然后选择 Plugins 插件,下载Maven Helper插件,如果安装之后不生效,可以重启一下 IDEA
在这里插入图片描述
最后打开 pom.xml 文件,选择 Dependency Analyzer 切换页,选择 All Dependencies as Tree 选项,然后搜索 javax,然后右键选择 Exclude 排除重复依赖就可以了。
在这里插入图片描述
点完之后会发现 pom.xml 文件中会出现如下代码

<dependency>
  <groupId>com.xxx.kas.swagger</groupId>
  <artifactId>swagger-spring-mvc-core</artifactId>
  <version>1.0-SNAPSHOT</version>
  <exclusions>
	<exclusion>
	  <artifactId>servlet-api</artifactId>
	  <groupId>javax.servlet</groupId>
	</exclusion>
  </exclusions>
</dependency>

如果排除之后依然报错,可以选择上图的Jump To Source跳转到对应的仓库内将对应版本删除即可。

刷新下依赖,发现正常启动了,搞定。

总结

以后再遇到这种包依赖冲突的问题都可以使用Maven Helper这个插件来轻松解决,妈妈再也不用担心我的学习了😃。

  • 92
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 155
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 155
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿Q说代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值