業余在一個SpringBoot項目集成Swagger2時,啟動過程一直出現以下報錯信息——
An attempt was made to call a method that does not exist. The attempt was made from the following location:
springfox.documentation.schema.DefaultModelDependencyProvider.dependentModels(DefaultModelDependencyProvider.java:79)
The following method did not exist:
com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;
The method's class, com.google.common.collect.FluentIterable, is available from the following locations:
jar:file:/C:/Users/92493/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar!/com/google/common/collect/FluentIterable.class
The class hierarchy was loaded from the following locations:
com.google.common.collect.FluentIterable: file:/C:/Users/92493/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.common.collect.FluentIterable
出現這個問題,大概猜測到,應該是Maven依賴沖突導致的,忽然想到,是否有可以檢查maven依賴沖突的插件呢,一查發現還真有,而且極方便。
這個Idea插件,叫Maven Helper,根據File -> Settings -> Plugins -> Marketplace 輸入Maven Helper即可找到。
下載后,重啟IDEA,這時點擊pom.xml文件,會發現多了一欄【Dependency Analyzer】——
切換至【Dependency Analyzer】欄,在搜索框輸入沖突包guava,即會出現,guava都被哪些包依賴了,當多個組件包都依賴了同一個包但又不同版本時,很容易久出現各種沖突。紅色部分即是導致啟動報異常的地方,可見,springfox-core已經依賴的是20.0包,但其他還依賴18版本的包,這里可以把報錯提示的18.0版本的通過exclusion去除即可。
選中對應想去除的包,右擊即可一鍵exclusion——
再切換至Text欄,即可發現已經自動增加了exclusions模塊——
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>