Spring解決依賴版本不一致報錯問題
問題描述
報錯信息如下
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-api和jakarta.servlet-api下的 ServletContext.class 沖突了,需要修改一下版本依賴。
解決方法
首先打開IDEA的 setting 設置

然后選擇 Plugins 插件,下載Maven Helper插件,如果安裝之后不生效,可以重啟一下 IDEA

最后打開 pom.xml 文件,選擇 Dependency Analyzer 切換頁,選擇 All Dependencies as Tree 選項,然后搜索 javax,然后右鍵選擇 Exclude 排除重復依賴就可以了。

點完之后會發(fā)現(xiàn) pom.xml 文件中會出現(xiàn)如下代碼
<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跳轉到對應的倉庫內將對應版本刪除即可。
刷新下依賴,發(fā)現(xiàn)正常啟動了,搞定。
總結
以后再遇到這種包依賴沖突的問題都可以使用Maven Helper這個插件來輕松解決,媽媽再也不用擔心我的學習了。
到此這篇關于Spring解決依賴版本不一致報錯問題的文章就介紹到這了,更多相關Spring版本不一致內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringSecurity角色權限控制(SpringBoot+SpringSecurity+JWT)
本文主要介紹了SpringSecurity角色權限控制(SpringBoot+SpringSecurity+JWT),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-05-05

