Tomcat 10 问题上的 Spring Boot 2.x

Posted

技术标签:

【中文标题】Tomcat 10 问题上的 Spring Boot 2.x【英文标题】:Spring Boot 2.x on Tomcat 10 Issue 【发布时间】:2022-01-22 16:02:38 【问题描述】:

我正在尝试在 Tomcat Docker 容器中部署一个 war 文件,但总是得到 404 Not Found 页面。我按照Intellij Tutorial创建了Spring Boot项目,它在我的本地机器上运行良好。

这是我的 Dockerfile:

FROM tomcat:latest
ADD WebTutorial.war /usr/local/tomcat/webapps/ROOT.war

我使用以下命令构建镜像并运行容器:

$ docker build -t sp-tomcat .
$ docker run -it -p 8080:8080 --name sp-tomcat sp-tomcat

容器运行后,我登录到容器中,看到war文件已经部署(我想):

这是日志输出:

21-Dec-2021 07:41:35.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.0.14
21-Dec-2021 07:41:35.821 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Dec 2 2021 22:01:36 UTC
21-Dec-2021 07:41:35.821 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.14.0
21-Dec-2021 07:41:35.821 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            5.10.76-linuxkit
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          aarch64
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.13+8
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
21-Dec-2021 07:41:35.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
21-Dec-2021 07:41:35.829 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
21-Dec-2021 07:41:35.830 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
21-Dec-2021 07:41:35.830 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
21-Dec-2021 07:41:35.830 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
21-Dec-2021 07:41:35.830 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
21-Dec-2021 07:41:35.831 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
21-Dec-2021 07:41:35.832 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
21-Dec-2021 07:41:35.833 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k  25 Mar 2021]
21-Dec-2021 07:41:35.979 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
21-Dec-2021 07:41:35.990 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [268] milliseconds
21-Dec-2021 07:41:36.018 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
21-Dec-2021 07:41:36.018 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.14]
21-Dec-2021 07:41:36.024 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/ROOT.war]
21-Dec-2021 07:41:36.576 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Dec-2021 07:41:36.596 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/ROOT.war] has finished in [572] ms
21-Dec-2021 07:41:36.599 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
21-Dec-2021 07:41:36.607 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [617] milliseconds

但是当我尝试通过浏览器查看该网站时没有运气。谁能告诉我我错过了什么?

【问题讨论】:

日志中有什么内容吗? less /usr/local/tomcat/logs/catalina.out 我在@Rüdiger 帖子中添加了容器日志的屏幕截图 请don't upload text as image。编辑您的问题以包含文本形式的所有信息 - 考虑使用编辑器的格式选项。 这能回答你的问题吗? Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not(提示:尝试tomcat 9) 您说您的应用程序在本地运行良好,但您是将其部署到 Tomcat 还是作为 Java 应用程序启动?我的猜测是:您的应用程序没有扩展 SpringBootServletInitializer 因此它不会在 Tomcat 上启动。 【参考方案1】:

问题在于您尝试在尚不支持的 Tomcat 10 上运行 Spring Boot 2.x 应用程序。 Tomcat 10 的最大变化之一是

从 Java EE 迁移到 Jakarta EE 作为将 Java EE 转移到 Eclipse Foundation 的一部分,所有已实现 API 的主要包已从 javax.* 更改为 jakarta.*。这几乎肯定需要更改代码以使应用程序能够从 Tomcat 9 及更早版本迁移到 Tomcat 10 及更高版本。

因此,Spring Framework 使用的所有 API 都基于旧的 javax.*,而新的 jakarta.* 将在 Spring Framework 6 和 Spring Boot 3 中支持 2022 年末))更多详细信息here

您需要做的只是调整 Dockerfile 以运行 Tomcat 9 的应用程序。如果您在 Java 17 上,这个可以工作

FROM tomcat:9.0-jre17-temurin
ADD WebTutorial.war /usr/local/tomcat/webapps/ROOT.war

【讨论】:

以上是关于Tomcat 10 问题上的 Spring Boot 2.x的主要内容,如果未能解决你的问题,请参考以下文章

部署在tomcat上的Spring Boot无法启动

Tomcat上的Spring Boot War部署不起作用

Tomcat 上的 Spring Security SAML 元数据 URL

Spring Boot 的 Keycloak 错误:tomcat 上的“不是子类型”

如何在运行在 Tomcat 上的 Spring Web 应用程序中使用 Spring 的响应式 WebClient

如何在 spring-boot 中拦截嵌入式 Tomcat 上的“全局”404