Java ApplicationContextException:由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50231736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:29:59  来源:igfitidea点击:

ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

javaspringspring-boot

提问by Vijayant Bhatia

I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception

我已经使用 Spring boot 编写了一个 spring 批处理应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在 linux 服务器上运行它时,它给了我以下异常

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException: 
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

Below is the way I am running it:

以下是我运行它的方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

采纳答案by Vijayant Bhatia

The solution is:

解决办法是:

I explicitly set the below property to nonein application.ymlfile.

我明确下面的属性设置为noneapplication.yml文件中。

spring:
  main:
    web-application-type: none

回答by GolamMazid Sajib

Case 1:

情况1:

@SpringBootApplicationannotation missing in your spring boot starter class.

@SpringBootApplication您的 Spring Boot 入门类中缺少注释。

Case 2:

案例2:

For non web application, disable web application typein properties file:

对于非 Web 应用程序,请web application type在属性文件中禁用:

In application.properties:

application.properties

spring.main.web-application-type=none

If you use application.ymlthen add:

如果您使用application.yml然后添加:

  spring:
    main:
      web-application-type: none

For web applications, extends *SpringBootServletInitializer*in main class.

对于 Web 应用程序,*SpringBootServletInitializer*在主类中扩展。

@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(YourAppliationName.class, args);
    }
}

Case 3:

案例3:

If you use spring-boot-starter-webfluxthen also add spring-boot-starter-webas dependency.

如果您使用spring-boot-starter-webfluxthen 也添加spring-boot-starter-web为依赖项。

回答by Michael

Upgrading spring-boot-starter-parentin pom.xmlto the latest version fixed it for me.

升级spring-boot-starter-parentpom.xml最新版本,固定对我来说。

回答by Evgeniy

I had this problem during migration to Spring Boot. I've found a advice to remove dependenciesand it helped. So, I removed dependency for jsp-apiProject had. Also, servlet-apidependency has to be removed as well.

我在迁移到 Spring Boot 时遇到了这个问题。我找到了删除依赖项建议,它有所帮助。所以,我删除了对jsp-api项目的依赖。此外, 还必须删除servlet-api依赖项。

compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2' 

回答by vishal thakur

Probably you missing @SpringBootApplicationin your spring boot starter class.

可能您错过@SpringBootApplication了 Spring Boot 入门课程。

@SpringBootApplication
public class LoginSecurityAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginSecurityAppApplication.class, args);
    }

}

回答by NemraOx

I encountered this problem when attempint to run my web application as a fat jarrather than from within my IDE (IntelliJ).

当我尝试将我的 Web 应用程序作为一个胖 jar而不是从我的 IDE (IntelliJ) 中运行时,我遇到了这个问题。

This is what worked for me. Simply adding a default profile to the application.propertiesfile.

这对我有用。只需将默认配置文件添加到application.properties文件即可。

spring.profiles.active=default

You don't have to use default if you have already set up other specific profiles (dev/test/prod). But if you haven't this is necessary to run the application as a fat jar.

如果您已经设置了其他特定配置文件 (dev/test/prod),则不必使用默认值。但是,如果您还没有,这对于将应用程序作为胖 jar 运行是必要的。

回答by Ravindranath Akila

Annotate class public static void mainwith, for example: @SpringBootApplication

注释类public static void main,例如:@SpringBootApplication

回答by adrhc

You probably use this in your project:

您可能会在您的项目中使用它:

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

in which case you'll have to also add:

在这种情况下,您还必须添加:

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

and the magic happens :)

奇迹发生了:)

PS: that's because Spring will use by default web-MVCinstead of web-fluxwhen both are available

PS:那是因为当两者都可用时,Spring 将默认使用web-MVC而不是web-flux

回答by Solomon Raja

In my case, the problem was I didn't had a Tomcat server separately installed in my eclipse. I assumed my Springboot will start the server automatically within itself.

就我而言,问题是我的 eclipse 中没有单独安装 Tomcat 服务器。我假设我的 Springboot 会自动启动服务器。

Since my main class extends SpringBootServletInitializerand override configuremethod, I definitely need a Tomcat server installed in my IDE.

由于我的主类扩展SpringBootServletInitializer和覆盖configure方法,我肯定需要在我的 IDE 中安装一个 Tomcat 服务器。

To install, first download Apachce Tomcat (version 9 in my case) and create server using Servers tab.

要安装,首先下载 Apache Tomcat(在我的例子中是版本 9)并使用 Servers 选项卡创建服务器。

After installation, run the main class on server.

安装后,在服务器上运行主类。

Run As -> Run on Server

回答by Spencer Sutton

My solution had to do with a bad dependency. I had:

我的解决方案与不良依赖有关。我有:

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

In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.

在我的 pom 中,我不得不注释掉排除以使其正常工作。出于某种原因,它必须寻找这个 tomcat 包。