1.下载的依赖不全
首先进入本地maven仓库,查看你项目中springboot版本的相关依赖,可以参考下面下面这张图(springboot的依赖在你maven配置文件设置的仓库目录下的“\org\springframework\boot\spring-boot-autoconfigure” ),如果缺少相关文件,你可以将当前目录及其子文件全部删除,然后进入项目,右键选择pom文件:Maven->Reimport
如果下载速度慢,可换成阿里的镜像仓库,具体步骤为打开maven的settings.xml文件,在mirrors结点下添加如下配置
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</mirror>
2.maven版本过高
如果你使用的maven版本过高的话,也可能会出现无法导入依赖的问题,尝试更换较低版本的maven,版本的对应关系可参考这条链接:https://maven.apache.org/docs/history.html
3.证书问题
通过下图方式打开IDEA的日志,如果能看到类似于下面的这条错误:
Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.1.8.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.1.8.RELEASE from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
具体可以参考这条链接:真正解决Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom
4.系统原因
如果通过以上三种方法无法解决,那很可能就是系统的原因了。我就碰到过因为系统长时间没有更新使得依赖无法导入的情况。
这种情况是我个人原因导致的。我用了一个软件成功关闭了Win10自动更新。打开系统设置这才发现,我当前用的Win10版本已经停止维护了,于是尝试将系统更新后,终于能够正常导入依赖了。
具体我使用什么办法关闭Win10自动更新的,可以看下面的这条链接:
https://iknow.lenovo.com.cn/detail/dc_178562.html
不得不说,这东西确实好用,用软件设置后,大半年没有更新过系统了。如果想关闭自动的小伙伴,可以试试。
其他
CMD进入项目目录或者在IDEA中打开Terminal,执行下面的命令,把下载不完全的依赖
mvn -U idea:idea
右键选择pom文件,重新导入即可