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

44

我是Spring Boot的新手,在运行我的应用程序时出现了错误。我正在遵循一个教程,我相信我的POM文件中已经正确地设置了父级和依赖项,请帮帮我。

主类:

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

错误为:

   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>

9
发布完整的堆栈跟踪。其中还有一行直接来自Tomcat,可能会指出您已经在该端口上打开了某个东西。 - chrylis -cautiouslyoptimistic-
13个回答

42

尝试在application.yaml (或 application.properties)中更改端口号为其他数字。


我找不到你所指的IDE目录。然而,当我以编程方式设置此端口时,它可以工作,尽管我必须在每次运行时更改端口...请建议如何解决此问题。 System.getProperties().put( "server.port", 130 ); - amar
请查看此链接:https://github.com/khoubyari/spring-boot-rest-example/blob/master/src/main/resources/application.yml - Aziz
虽然我的应用程序没有这个配置文件。TCP端口冲突是问题的原因,所以这篇文章仍然非常有帮助。主要错误是:无法启动与ProtocolHandler相关联的终结点和java.net.BindException:地址已在使用中。 - Josiah DeWitt

15
你需要在你的pom文件中添加tomcat的依赖。
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

2
我有这个依赖项,但仍然出现异常org.springframework.boot.web.server.WebServerException:无法启动嵌入式Tomcat。 - Pablo Pazos

5

如果您在Linux环境下运行应用程序,则基本上您的应用程序没有默认端口的权限。

尝试通过在VM上使用以下选项来使用8181端口。

-Dserver.port=8181


4

在我的情况下,我遇到了多个错误,其中第一个是:“无法启动嵌入式Tomcat Servlet容器”。我尝试多次解决第一个问题,但实际问题出现在其他错误中。结果发现我导入了两个不同的Logger,因此当我尝试初始化它时,没有编译问题,但却是一个运行时错误。

我的建议:检查您所有的错误,也许问题不在我最初认为的第一个错误上。因此,这个问题的解决方案可能在下一个显示的错误中。

如果这是你唯一的错误,解决方案可能是以上答案中的一个。


2
在我遇到“无法启动嵌入式Tomcat Servlet容器”异常时,我通过在application.properties中添加debug=true来打开Spring Boot的调试模式,然后重新运行代码,它告诉我java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String。因此,我们知道可能是我正在使用较低版本的servlet API,并且与Spring Boot版本冲突。我查看了我的pom.xml文件,并发现其中一个依赖项正在使用servlet2.5,所以我将其排除。现在它可以正常工作了。希望这能有所帮助。

2

对于我来说,我只需在mvn后面加上-X标志。查看调试日志; Spring在定位.properties文件时出现了问题。


2

这可能是由于项目中Java版本的更改导致的。例如,如果项目是在Java 8中构建的,如果我们将Java版本更改为11,则可能会出现此类问题。在Intellij Idea中,转到文件->项目结构,然后更改项目SDK版本


0
你需要添加Tomcat依赖,并且将你的应用程序类继承自extends SpringBootServletInitializer。
@SpringBootApplication  
public class App extend SpringBootServletInitializer
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, "hello");
    }
}

0

如上所述,如果您正在运行Linux,则应用程序无法访问80端口。有两种解决此问题的方法:

  1. 以root权限运行应用程序-仅适用于本地测试,不适用于任何生产环境。

  2. 例如在8081端口上运行应用程序,使用nginx设置反向代理,并将请求从80重定向到8081。


0

处理这个问题的简单方法是在您的application.properties或.application.yml文件中包含以下内容:server.port=0(对于application.properties)和server.port: 0(对于application.yml文件)。当然,需要注意这些可能会根据您使用的springboot版本而改变。

这将允许您的计算机动态分配任何可用的空闲端口。

要静态分配端口,请将上述更改为server.port = someportnumber。如果运行基于Unix的操作系统,则可能需要检查有关该端口的僵尸活动,并尽可能使用fuser -k {theport}/tcp来终止它。

您的.yml或.properties应如下所示:

server: port: 8089 servlet: context-path: /somecontextpath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接