【问题标题】:Tomcat Doesn't Come Up When Running Spring Boot Application Through the Jar (missing ServletWebServerFactory bean)通过 Jar 运行 Spring Boot 应用程序时 Tomcat 不出现(缺少 ServletWebServerFactory bean)
【发布时间】:2021-02-04 13:05:27
【问题描述】:

我遇到了一个奇怪的问题。我有一个监听 8080 的 Spring Boot 应用程序。这是我的配置:

  1. @SpringBootApplication 已配置。
  2. spring-boot-starter-parentspring-boot-starter-web 出现在我的 pom 中。

当我通过 Intellij 运行应用程序时,应用程序运行良好。但不是通过我使用maven 程序集插件创建的胖罐子。我必须指定

@Bean
  public ServletWebServerFactory servletWebServerFactory() {
    return new TomcatServletWebServerFactory();
  }

让 jar 运行。

我的 POM

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <parent>
    <artifactId>parent</artifactId>
    <groupId>com.pandepra.tasks</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>deploy</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.pandepra.tasks</groupId>
      <artifactId>module</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-couchbase</artifactId>
    </dependency>
    <dependency>
      <groupId>com.couchbase.client</groupId>
      <artifactId>java-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    <dependency>
      <groupId>io.projectreactor</groupId>
      <artifactId>reactor-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
      <groupId>com.github.tomakehurst</groupId>
      <artifactId>wiremock</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.pandepra.deploy.Application</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
      </plugin>
    </plugins>
  </build>


  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

</project>

堆栈跟踪:

18:26:51.150 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.pandepra.deploy.Application.main(Application.java:10)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
    ... 8 common frames omitted

我的父 POM:

<?xml version="1.0" encoding="UTF-8"?>
<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.pandepra.tasks</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>traces</module>
    <module>common</module>
  </modules>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>com.balajeetm.mystique</groupId>
        <artifactId>json-mystique-starter</artifactId>
        <version>2.2.0</version>
      </dependency>
      <dependency>
        <groupId>com.balajeetm.mystique</groupId>
        <artifactId>json-mystique</artifactId>
        <version>2.2.0</version>
      </dependency>
      <dependency>
        <groupId>com.balajeetm.mystique</groupId>
        <artifactId>jackson-utils</artifactId>
        <version>2.2.0</version>
      </dependency>
      <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>1.58</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <profiles>
    <profile>
      <id>traces</id>
      <modules>
        <module>
          traces
        </module>
      </modules>
    </profile>
  </profiles>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

</project>

【问题讨论】:

  • 请包含完整的堆栈跟踪。
  • @GiorgiTsiklauri 添加了。
  • 你在 pom.xml 中的parent 是什么?
  • 那是我定义依赖管理部分的父 pom。我更改了 artifactId 以隐藏不必要的信息。我已经添加了父 POM。谢谢。
  • 好吧,如果您没有按照应有的方式定义父 pom,怎么会没有必要?

标签: java spring spring-boot tomcat


【解决方案1】:

将项目打包到 .war,然后部署到 tomcat

https://developer.okta.com/blog/2019/04/16/spring-boot-tomcat

【讨论】:

  • 你试过 ./mvnw spring-boot:run 命令了吗?
  • 我认为在运行嵌入式 tomcat 时不需要。我将其部署为 docker 容器。
【解决方案2】:
You can use spring-boot maven plugin,

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

Run app using following command,
mvn spring-boot:run 

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2019-08-07
    • 2019-12-03
    • 2020-08-08
    • 1970-01-01
    • 2023-01-28
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多