SpringBoot - 无法启动嵌入式容器

VNT*_*VNT 7 java maven spring-boot

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

   .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: 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]
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

VNT*_*VNT 8

通过使用@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,通过添加@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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*han 6

尝试使用注释注释您的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);
    }
}
Run Code Online (Sandbox Code Playgroud)


tom*_*tom 5

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

@SpringBootApplication
public class Application implements CommandLineRunner {
//my code here
}
Run Code Online (Sandbox Code Playgroud)

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

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

但是我也遇到了这个错误,并且在pom中没有真正相关的与Web相关的依赖。

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

如果您检查springboot的源代码,它将在启动应用程序时检查项目中是否有“ javax.servlet.Servlet”。并在存在任何“ javax.servlet.Servlet”时尝试启动Web“嵌入式容器”。

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

因此,解决方案非常简单,只需在“ application.properties”中告诉springboot这不是一个Web项目:

spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)