Java Spring boot:无法启动嵌入式Tomcat servlet容器

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

Spring boot: Unable to start embedded Tomcat servlet container

javaspringspring-mvctomcatspring-boot

提问by amar

I'm new to Spring Boot and having with error while running my application. I'm following a tutorial and I believe I'm having proper parent and dependencies with POM, please help me

我是 Spring Boot 的新手,并且在运行我的应用程序时出错。我正在学习教程,我相信我对 POM 有适当的父级和依赖项,请帮助我

main class:

主类:

package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Hello world!
 *
 */

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, "hello");
    }
}

error is:

错误是:

   org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at com.boot.App.main(App.java:18) [classes/:na]Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:159) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        ... 10 common frames omitted

POM :

聚甲醛:

    <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.boot</groupId>
  <artifactId>das-boot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>
  <name>das-boot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

采纳答案by Aziz

Try to change the port number in application.yaml(or application.properties) to something else.

尝试将application.yaml(或application.properties) 中的端口号更改为其他内容。

回答by cralfaro

You need to add the tomcat dependency in your pom

您需要在 pom 中添加 tomcat 依赖项

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

回答by Chids

You need to Tomcat Dependency and also extend your Application Class from extends SpringBootServletInitializer

您需要 Tomcat Dependency 并从 extends SpringBootServletInitializer 扩展您的应用程序类

@SpringBootApplication  
public class App extend SpringBootServletInitializer
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, "hello");
    }
}

回答by Norman Bai

In my condition when I got an exception " Unable to start embedded Tomcat servlet container",

在我遇到异常“无法启动嵌入式 Tomcat servlet 容器”的情况下,

I opened the debug mode of spring boot by adding debug=truein the application.properties,

我通过debug=true在application.properties中添加开启spring boot的调试模式,

and then rerun the code ,and it told me that java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String

然后重新运行代码,它告诉我 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String

Thus, we know that probably I'm using a servlet API of lower version, and it conflicts with spring boot version.

因此,我们知道可能我正在使用较低版本的servlet API,并且它与spring boot版本冲突。

I went to my pom.xml, and found one of my dependencies is using servlet2.5, and I excluded it.

我去了我的 pom.xml,发现我的一个依赖项是使用 servlet2.5,我把它排除在外。

Now it works. Hope it helps.

现在它起作用了。希望能帮助到你。

回答by Jawad Khan

For me, I just had to put a -X flag with mvn. Look at the debug log; there was an issue with Spring while locating the .properties file.

对我来说,我只需要在mvn. 查看调试日志;查找 .properties 文件时,Spring 存在问题。

回答by Amos Kosgei

Simple way to handle this is to include this in your application.properties or .yml file: server.port=0for application.properties and server.port: 0for application.yml files. Of course need to be aware these may change depending on the springboot version you are using. These will allow your machine to dynamically allocate any free port available for use. To statically assign a port change the above to server.port = someportnumber. If running unix based OS you may want to check for zombie activities on the port in question and if possible kill it using fuser -k {theport}/tcp. Your .yml or .properties should look like this. server: port: 8089 servlet: context-path: /somecontextpath

处理此问题的简单方法是将其包含在您的 application.properties 或 .yml 文件中: server.port=0对于 application.properties 和server.port: 0application.yml 文件。当然需要注意这些可能会根据您使用的 springboot 版本而改变。这些将允许您的机器动态分配任何可用的可用端口。要静态分配端口,请将上述更改为server.port = someportnumber. 如果运行基于 Unix 的操作系统,您可能需要检查相关端口上的僵尸活动,并在可能的情况下使用fuser -k {theport}/tcp. 您的 .yml 或 .properties 应如下所示。 server: port: 8089 servlet: context-path: /somecontextpath