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

我是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:

   4.0.0 com.boot das-boot 0.0.1-SNAPSHOT jar  org.springframework.boot spring-boot-starter-parent 1.3.1.RELEASE  das-boot http://maven.apache.org  UTF-8    org.springframework.boot spring-boot-starter-web   junit junit 3.8.1 test    

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

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

  org.springframework.boot spring-boot-starter-tomcat  

步骤1

以管理员身份运行命令行。 然后运行下面提到的命令。 在yourPortNumber中键入您的端口号

netstat -ano | findstr:yourPortNumber

在此处输入图像描述

红色圆圈区域显示PID(进程标识符)

第2步

然后在识别PID后执行此命令。

taskkill / PID typeyourPIDhere / F.

在此处输入图像描述

PS再次运行第一个命令以检查进程是否仍然可用。 如果进程成功结束,您将获得空行。

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

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

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

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

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

现在它有效。 希望能帮助到你。

您需要Tomcat Dependency并从extends SpringBootServletInitializer扩展您的Application类

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

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

处理此问题的简单方法是将其包含在application.properties或.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