IDEA启动Spring Boot项目提示Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet...

18 篇文章 0 订阅
11 篇文章 0 订阅

导入一个gradleSpring Boot项目,在启动的时候先提示找不到HttpServletRequest这个包,错误如下:

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_45]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_45]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_45]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_45]
    ... 29 common frames omitted

但是相应的Java代码没有报错,所以单独找了servlet-api.jar导入,并将其添加到Module中,但是导入后出现另一个错误:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
....

找了很久都没有找到解决的方法,但是使用gradle bootrun是可以正常启动运行的,在另外一台机子上也没有任何问题,所以认为项目本身没有任何问题,是在IDEA启动的过程中出现了问题导致的,

看到有一篇使用Maven也遇到该问题的帖子,对比了依赖:

dependencies {
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.2.0')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    // this is for generate war file
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}

然后将providedRuntime改成了runtime,重新build启动,没有任何问题
该问题产生的原因很可能是因为IDEA在启动的过程中并没有像Gradle一样做完全的build,只是进行了热更新,没有将需要的Jar包编译到项目里

providedCompileprovidedRuntime。虽然它们有各自的compileruntime 配置,但这些配置有相同的作用域,只是它们不会添加到 war/jar 文件中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值