【问题标题】:SpringBoot - Unable to start embedded containerSpring Boot - 无法启动嵌入式容器
【发布时间】:2017-04-17 08:20:49
【问题描述】:

当我启动 springboot 应用程序时,我的 SpringBootLoginController 类抛出此错误(无法启动嵌入式容器),如下所示。这是一个 hello world 类型的 spring boot 应用程序示例。

   .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.2.RELEASE)

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]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]

我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test.springboot</groupId>
    <artifactId>HelloSpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>HelloSpringBoot</name>
    <description>HelloSpringBoot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

我的控制器:

import org.springframework.boot.*;
import org.springframework.web.bind.annotation.*;

@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

}

【问题讨论】:

    标签: java maven spring-boot


    【解决方案1】:

    使用 @SpringBootApplication 进行注释可解决此问题。

    @SpringBootApplication
    @RestController
    public class SpringBootLoginController {
    
        @RequestMapping("/hello")
        String hello() {
            return "Hello World!!!";
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootLoginController.class, args);
        }
    }
    

    或者,通过添加 @EnableAutoConfiguration 也可以解决此问题。

    @EnableAutoConfiguration
    @RestController
    public class SpringBootLoginController {
    
        @RequestMapping("/hello")
        String hello() {
            return "Hello World!!!";
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootLoginController.class, args);
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用@SpringBootApplication 注释来注释您的SpringBootLoginController 类。

      @SpringBootApplication
      @RestController
      public class SpringBootLoginController {
      
          @RequestMapping("/hello")
          String hello() {
              return "Hello World!!!";
          }
      
          public static void main(String[] args) throws Exception {
              SpringApplication.run(SpringBootLoginController.class, args);
          }
      }
      

      【讨论】:

        【解决方案3】:

        就我而言,我正在使用 springboot 开发一个命令行项目。

        @SpringBootApplication
        public class Application implements CommandLineRunner {
        //my code here
        }
        

        所以我只是使用简单的启动器。

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

        但是我也遇到了这个错误,并且我的 pom 中没有与 web 相关的依赖项,这确实是有线的。

        最后我发现我的一个依赖项目在它自己的 pom 中使用了“javax.servlet.Servlet”。

        如果你查看springboot的源代码,它会在启动应用程序时检查你的项目中是否有“javax.servlet.Servlet”。并在有任何“javax.servlet.Servlet”时尝试启动一个网络“嵌入式容器”。

        这就是我收到此错误的原因,因为我使用的是“spring-boot-starter”并且其中没有 Web 容器。

        所以解决方法很简单,只需在“application.properties”中告诉springboot这不是web项目即可:

        spring.main.web-environment=false
        

        【讨论】:

          【解决方案4】:

          我删除了我的 .m2 存储库,将我的版本从 1.3.1 更改为 2.1.3,进行了 maven 清理,重新构建项目并首次运行(使用 intellij)

          parent>
             <groupId>org.springframework.boot</groupId>
             <version>2.1.3.RELEASE</version>
             <artifactId>spring-boot-starter-parent</artifactId>
          </parent>
          

          【讨论】:

          • 嘿,马克,这个问题已有 2 年多的历史了 - 如果您想将精力集中在新问题上,可以使用“最新”选项卡来实现。例如,this 是最新 Maven 问题的链接。
          • 公平点,刚刚添加了一个答案,因为我遇到了同样的问题,并以一种看起来像新的方式修复了它。作为一个新手,我肯定很快就会得到stackoverflow的想法!
          【解决方案5】:

          org.springframework.boot的版本从1.4.2改为2.1.3

          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.1.3.RELEASE</version>
          

          【讨论】:

            【解决方案6】:

            在我的情况下添加

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
            

            已解决的问题

            【讨论】:

              【解决方案7】:

              您应该注释您的 SpringBootLoginController 类。阅读 @SpringBootApplication 注释。

              @SpringBootApplication
              @RestController
              public class SpringBootLoginController {
              
                  @RequestMapping("/hello")
                  String hello() {
                      return "Hello World!!!";
                  }
              
                  public static void main(String[] args) throws Exception {
                      SpringApplication.run(SpringBootLoginController.class, args);
                  }
              }
              

              【讨论】:

                【解决方案8】:

                如果您使用的是 maven,则由

                构建
                mvn package
                

                而不是 IDE 构建 jar。

                在我的例子中,IDE 构建的 jar 存在很多问题。

                【讨论】:

                  【解决方案9】:

                  直到现在我还没有弄清楚这个问题,但我删除了我的 .m2 存储库,提供了安装版本,一切都很顺利

                  【讨论】:

                    【解决方案10】:

                    您的依赖项有问题。添加这个

                    <dependency>
                    <groupId><groupId></groupId>
                    <artifactId><some dependency></artifactId>
                    <version><version></version>
                    <exclusions>
                        <exclusion>
                            <groupId>javax.servlet</groupId>
                            <artifactId>servlet-api</artifactId>
                        </exclusion>
                    </exclusions>
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2017-09-20
                      • 2020-03-25
                      • 1970-01-01
                      • 2016-08-19
                      • 1970-01-01
                      • 2016-12-29
                      • 2015-09-06
                      • 1970-01-01
                      相关资源
                      最近更新 更多