Java SpringBoot - 无法启动嵌入式容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43447798/
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-12 00:54:26  来源:igfitidea点击:

SpringBoot - Unable to start embedded container

javamavenspring-boot

提问by VNT

My SpringBootLoginController class throw this error( unable to start embedded container) which is shown below when i have started the springboot application.It is a hello world kind spring boot application example.

当我启动 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]

My pom.xml

我的 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>

My Controller :

我的控制器:

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

}

采纳答案by VNT

Annotating with the @SpringBootApplicationresolves this issue.

使用@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);
    }
}

Alternatively , by Adding the @EnableAutoConfigurationalso resolves this issue.

或者,通过添加@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);
    }
}

回答by Abdullah Khan

Try annotating your SpringBootLoginControllerclass with @SpringBootApplicationannotation.

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

回答by Jorge Machado

you have a problem on your dependencies. Add this

你的依赖有问题。添加这个

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

回答by THM

in my case adding

在我的情况下添加

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

solved issue

解决的问题

回答by tom

For my case, I was developing a command line project with springboot.

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

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

So I was just using the simple starter.

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

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

But I also got this error, and there was no web related dependency in my pom which was really wired.

但是我也遇到了这个错误,并且我的 pom 中没有真正连接的网络相关依赖项。

And at last I found out that one of my dependency project was using the "javax.servlet.Servlet" in its own pom.

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

If you check the source code of the springboot, it will check if there is any "javax.servlet.Servlet" in your project when starting the application. And try to start a web "embedded container" when there is any "javax.servlet.Servlet".

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

That's why I got this error because I was using the "spring-boot-starter" and there was no web container in it.

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

So the solution is very simple, just tell the springboot that this is not a web project in the "application.properties":

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

spring.main.web-environment=false

回答by devOps

You should annotate your SpringBootLoginControllerclass. Read about @SpringBootApplicationannotation.

你应该注释你的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);
    }
}

回答by NeoSoNg

If you are using maven then build by

如果您使用的是 maven,则通过构建

mvn package

instead of IDE build jar.

而不是 IDE 构建 jar。

There are many problems with the IDE built jar in my case.

在我的情况下,IDE 构建的 jar 有很多问题。

回答by Siena

Till now i haven't figured out the issue, but I deleted my .m2 repo, gave the install build and it all worked liked charm

直到现在我还没有弄清楚这个问题,但我删除了我的 .m2 存储库,提供了安装版本,一切都像魅力一样工作

回答by Mark Gibson

I deleted my .m2 repo, changed my version from 1.3.1 to 2.1.3 for the below, did a maven clean, re-build the project and it ran first time (using intellij)

我删除了我的 .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>

回答by Pavani Reddi

Change the version of the org.springframework.bootto 2.1.3 from 1.4.2

将版本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>