Spring启动 - java.lang.ClassNotFoundException:javax.servlet.ServletContext并且无法启动EmbeddedWebApplicationContext

时间:2016-06-21 07:39:07

标签: grails spring-boot

我有一个spring boot应用程序(grails 3.1.8),但是当我在intellij中运行应用程序时,我收到以下错误:

Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:232)
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:510)
    ... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    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.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152)
    ... 29 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    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)
    ... 33 common frames omitted

我尝试按照Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

中的建议添加javax-servlet-api依赖项

然后我开始得到:

ERROR org.springframework.boot.SpringApplication - Application startup failed
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:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:55)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:374)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:363)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at closemytab.Application.main(Application.groovy:13)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 18 common frames omitted

这篇文章建议删除javax-servlet-api:Spring boot -- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

然后我面对原来的问题。

有什么想法吗?

6 个答案:

答案 0 :(得分:28)

问题在于build.gradle

    compile "org.springframework.boot:spring-boot-starter-tomcat"

Intellij对所提供的

感到不满意

一切换到

'thin columns = gray
   range("E2:E46, I2:I46, M2:M46, Q2:Q46, U2:U46, Y2:Y46, AC2:AC46, AG2:AG46, AK2:AK46, AO2:AO46, AS2:AS46").Select
    Selection.BorderAround Weight:=xlThin, LineStyle:= xlContinuous
   With Selection.Interior
    .Color = 11711154
    End With

应用程序工作

答案 1 :(得分:6)

使用maven spring boot目标运行: spring-boot:run

在IntelliJ中设置maven配置的步骤:

调试/运行配置|点击左上方可见的+按钮|选择Maven |将命令行设置为spring-boot:run

答案 2 :(得分:2)

在下面添加依赖项解决了我的问题:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

请记住也要具有父级依赖项!

答案 3 :(得分:2)

如果您使用的是 intelliJ idea,则需要在 Run -> Edit Configurations -> Application 'your main class' 中启用“包含具有“Provider”范围的依赖项(默认情况下它是禁用的) enter image description here

答案 4 :(得分:0)

对我来说,这是因为tomcat版本。我将Tomcat的版本从7.5更改为8.5,问题已解决。

答案 5 :(得分:0)

Exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext仅表示您不向应用程序提供tomcat依赖项。如果您使用的是intelliJ Idea,做到这一点非常简单,如下所示。

选择项目->打开Maven Pannel(它大部分位于面板的右上角)->选择嵌入式。

Click here for Screenshot