Spring Boot - 让人抓狂的ClassNotFoundException

我们的项目基于Spring Boot + Maven,采用Intelli IDEA工具进行开发。代码在另一名同事的机器上检出之后,启动项目时,始终出现如下错误提示:
<code>
03-11 15:41:23[org.springframework.boot.SpringApplication-825][main][13298] – Application startup failed
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113)
at cn.com.icaifeng.Application.main(Application.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletResponse
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 29 more
</code>
开始怀疑是JDK或JRE设置的问题,但项目完全可以编译成功,所以此点可以排除。从错误信息:<code> javax/servlet/http/HttpServletResponse</code>可以看是Servlet的问题,Sevlet是java的web server一个容器,那么是不是表示这个容器类无法加载进行初始化?利用bing及百度做了一些查找(不要问为什么不用google),并未找到解决方案。折腾了三个小时之后,最终放弃了直接解决这个问题。让同事重新安装了Intell IDEA,并重新下载代码进行编译运行,居然一切正常,真让人想哭又想笑!自己内心松了一口气,虽然并未找到真正的原因,但终究这个问题消失了。

“欠下的债,终究要还的!”在没过两日的今天,在自己的机器居然也出现了这种问题,折腾近一个小时仍旧没有找到原因与解决办法,真是有点让人奔溃,有点想放弃了。但转念一想,如果今日放弃,想必日后肯定还会再次遇到,何不今日多花点时间,彻底弄清这个问题,找出原因与解决方案。

没有了内心的烦躁与焦虑,自己反而能够静心地把错误的信息想了又想,推测各种可能的原因。加上之前的种种尝试的失败,基本断定是web server容器的问题。上网查了一些资料,发现如果容器出现问题,确实会报此种错误。项目使用了spring boot的内嵌tomcat作为web server,那么很有可能是这个容器出现了问题。虽然Maven也加载了spring boot的tomcat包,但是不是这个包损坏或存在问题。在项目的pom文件,添加了以下代码:
<code>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
</code>
更新了Maven的依赖包,重新编译运行,奇迹发生了,一切OK,内心真是有点小激动!

这不是一个大问题,但我还是想把它记录下来,分享给以后可能遇到这个问题的朋友,节省他们的时间,早日实现人生巅峰的梦想_

    原文作者:杨梅泡酒
    原文地址: https://www.jianshu.com/p/665867400f12
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞