Spring Boot - java.lang.类未找到异常:javax.servlet.servletContext并且无法启动嵌入式Web应用程序上下文

ne5o7dgx  于 2023-01-09  发布在  Spring
关注(0)|答案(6)|浏览(196)

我有一个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

我尝试按照构造函数抛出异常中的建议添加javax-servlet-api依赖项;嵌套异常为java. lang. NoClassDefFoundError:javax/服务小程序/服务上下文
但后来我开始得到:

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
但我还是要面对最初的问题。
有什么想法吗?

tjrkku2a

tjrkku2a1#

问题出在build.gradle上

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

Intellij对提供的不满意
我一换成

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

应用程序工作

l7wslrjt

l7wslrjt2#

穿着MavenSpring Boot 目标:第一个月
在IntelliJ中设置maven配置的步骤:
调试/运行配置|点击左上方可见的+按钮|选择Maven|将命令行设置为spring-boot:run

pwuypxnk

pwuypxnk3#

如果您正在使用intelliJ idea,您需要在 Run -〉Edit Configurations -〉Application 'your main class' 中启用' Include dependencies with“Provider”scope(包含具有“提供者”作用域的依赖项)(默认情况下它是禁用的)

v1l68za4

v1l68za44#

在下面添加依赖项修复了我的问题:

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

记住也要有父依赖关系!

wswtfjt7

wswtfjt75#

对我来说,这是因为tomcat版本。我把tomcat版本从7.5改为8.5,问题就解决了。

ttcibm8c

ttcibm8c6#

Exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext仅仅意味着你不需要为你的应用程序提供tomcat依赖项,如果你使用intelliJ Idea,那么做起来很简单,如下所示。
选择你的项目-〉打开Maven面板(它通常位于面板的右上角)-〉选择嵌入式。

相关问题