ClassNotFoundException报错
- 具体问题
- 报错详情
- 推测可能是jar包问题
- 问题总结自定义依赖缺少依赖jar
具体问题
Caused by: java.lang.NoClassDefFoundError: retrofit2/converter/simplexml/SimpleXmlConverterFactory
报错详情
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: retrofit2/Retrofit$Builderat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1006)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)...
java.lang.NoClassDefFoundError: retrofit2/Retrofit$Builder
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1006)
推测可能是jar包问题
Retrofit是一个自定义jar包中的pom依赖(jar包被引入到项目中依赖)。
<dependency><groupId>com.squareup.retrofit2</groupId><artifactId>retrofit</artifactId><version>2.5.0</version></dependency>
打开terminal,定位到工程pom.xml所在的目录,执行 mvn dependency:tree 打印maven依赖关系树 。
mvn dependency:tree
如果你的操作系统环境定义了多个本地maven repo的配置,则需要在后面带上你的setttings.xml配置。
例如:
mvn dependency:tree -settings C:\Users\z00402970\2.0_iocm\settings.xml
打印结果:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.ering.van:van-share >-----------------------
[INFO] Building van-lte-boot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ van-share ---
[INFO] net.ering.van:van-share:jar:0.0.1-SNAPSHOT
[INFO] +- org.testng:testng:jar:6.9.10:test
[INFO] | +- com.beust:jcommander:jar:1.48:test
[INFO] | \- org.beanshell:bsh:jar:2.0b4:test
[INFO] +- org.mockito:mockito-all:jar:1.10.19:test
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.0.4.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.0.4.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.4.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
...
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.321 s
[INFO] Finished at: 2020-10-14T15:28:11+08:00
[INFO] ------------------------------------------------------------------------
打印结果中检索servlet
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
发现其他的基本都是compile,而servlet是provided结尾,是项目pom中加上了scope限制
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${servlet-version}</version><scope>provided</scope></dependency>
回到报错信息第一行,鼠标点击显示如下:
说明tomcat中也有servlet-api包,provided可以避免发生冲突。
因为provided表示该包只在编译和测试的时候用,所以,当启动tomcat的时候,tomcat中的servlet-api就不会和javax.servert的依赖冲突了。
这个provided的只在编译和测试期有效,已提供范围的依赖在编译classpath 可用,运行时不可用。并且不被传递。
问题总结自定义依赖缺少依赖jar
最后发现是引入的自定义依赖的源码缺少了Retrofit的依赖的引入。