部分翻译如下: 尝试部署简单的Hello Spring Boot应用时出现Tomcat 404错误。

huangapple go评论100阅读模式
英文:

Getting Tomcat 404 error while trying to deploy simple hello spring boot app

问题

我认为我有类似的问题,但不知道如何修复它。

我正在尝试使用Docker和Tomcat构建一个简单的Hello World Spring Boot应用程序。我使用Maven生成了war文件,然后将其放置在$CATALINA_HOME/webapps/目录下,并通过执行["catalina.sh", "run"]来启动服务器。但我仍然无法访问我的应用程序。

错误信息:
HTTP状态 404 - 未找到
类型 状态报告
消息 /demo/
描述 原始服务器未找到目标资源的当前表示,或者不愿透露其存在。
Apache Tomcat/9.0.2

Tomcat显示我的应用程序正在运行。

我尝试发出另一个请求以避免错误的映射:
http://localhost:8888/demo/hello
http://localhost:8888/hello

主类代码:
@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(DemoApplication.class);
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/hello")
	public String hello() {
		return "Hello";
	}

}

Dockerfile:
...
(Dockerfile内容)

pom.xml:
...
(pom.xml内容)

Tomcat日志:
...
(Tomcat日志内容)
英文:

I think I have similar problem, but don't know how to fix it
https://stackoverflow.com/questions/43186315/tomcat-404-error-the-origin-server-did-not-find-a-current-representation-for-th

Error image

I'm trying to build simple hello world spring boot app using docker and Tomcat. I made war file with maven, then put it to $CATALINA_HOME/webapps/ and started the server by performing ["catalina.sh", "run"]. But I still can't access to my app.

Tomcat status image

Error:

HTTP Status 404 – Not Found

Type Status Report

Message /demo/

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.2

Tomcat says, that my app is running

I tried to make another request to avoid wrong mapping
http://localhost:8888/demo/hello
http://localhost:8888/hello

Main class code:

@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(DemoApplication.class);
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/hello")
	public String hello() {
		return "Hello";
	}

}

Dockerfile

#
# for build
#
FROM maven:3.6.3-jdk-11-slim AS MAVEN_TOOL_CHAIN

COPY pom.xml /tmp/

COPY src /tmp/src/

WORKDIR /tmp/

RUN mvn package


#
# for deploy
#
FROM tomcat:9.0-alpine

MAINTAINER "admin"

USER root

# to get access to admin page
COPY /tomcat-users.xml $CATALINA_HOME/conf/tomcat-users.xml
COPY /settings.xml $CATALINA_HOME/conf/settings.xml
COPY /context.xml /usr/local/tomcat/webapps/manager/META-INF/

WORKDIR $CATALINA_HOME/webapps/

COPY --from=MAVEN_TOOL_CHAIN /tmp/target/demo-0.0.1-SNAPSHOT.war ./demo.war

EXPOSE 8080

CMD ["catalina.sh", "run"]

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
		<start-class>com.example.demo.DemoApplication</start-class>
	</properties>

	<dependencies>
		<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>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

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

</project>

Tomcats logs

26-Oct-2020 18:47:52.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/9.0.2
26-Oct-2020 18:47:52.613 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Nov 25 2017 21:08:02 UTC
26-Oct-2020 18:47:52.613 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         9.0.2.0
26-Oct-2020 18:47:52.613 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.19.76-linuxkit
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-1.8-openjdk/jre
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_151-b12
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
26-Oct-2020 18:47:52.614 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
26-Oct-2020 18:47:52.615 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
26-Oct-2020 18:47:52.617 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
26-Oct-2020 18:47:52.617 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
26-Oct-2020 18:47:52.617 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
26-Oct-2020 18:47:52.617 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
26-Oct-2020 18:47:52.617 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
26-Oct-2020 18:47:52.618 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
26-Oct-2020 18:47:52.618 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
26-Oct-2020 18:47:52.618 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.16] using APR version [1.6.3].
26-Oct-2020 18:47:52.618 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
26-Oct-2020 18:47:52.618 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
26-Oct-2020 18:47:52.622 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.0.2n  7 Dec 2017]
26-Oct-2020 18:47:52.740 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
26-Oct-2020 18:47:52.756 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Oct-2020 18:47:52.766 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
26-Oct-2020 18:47:52.767 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Oct-2020 18:47:52.768 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 607 ms
26-Oct-2020 18:47:52.795 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
26-Oct-2020 18:47:52.795 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.2
26-Oct-2020 18:47:52.829 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/demo.war]
26-Oct-2020 18:47:54.207 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.
26-Oct-2020 18:47:54.332 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/demo.war] has finished in [1,502] ms
26-Oct-2020 18:47:54.334 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
26-Oct-2020 18:47:54.361 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [28] ms
26-Oct-2020 18:47:54.362 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
26-Oct-2020 18:47:54.384 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [22] ms
26-Oct-2020 18:47:54.384 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
26-Oct-2020 18:47:54.556 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [171] ms
26-Oct-2020 18:47:54.556 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
26-Oct-2020 18:47:54.568 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [12] ms
26-Oct-2020 18:47:54.569 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
26-Oct-2020 18:47:54.581 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [12] ms
26-Oct-2020 18:47:54.585 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
26-Oct-2020 18:47:54.610 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
26-Oct-2020 18:47:54.612 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1844 ms
26-Oct-2020 18:47:54.292 INFO [main] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
26-Oct-2020 18:47:54.550 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
26-Oct-2020 18:47:54.550 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()

答案1

得分: 2

你正在使用的 Tomcat 镜像使用了 jdk8 作为 Java 版本,但你构建的 WAR 文件使用了 java11。因此,Tomcat 未能部署你的应用程序。
只需使用适用于 Tomcat 的 jdk11 镜像,你的应用程序应该可以正常运行。
你可以使用以下镜像:

FROM tomcat:9.0-jdk11

为了减少漏洞,Docker 镜像中的 webapps 文件夹将为空,但用于管理和其他操作所需的文件夹将存储在 webapps.dist 文件夹中。因此,如果你需要管理器和首页界面,只需将项目从 webapps.dist 复制到 webapps 目录。

RUN cp -r $CATALINA_HOME/webapps.dist/* $CATALINA_HOME/webapps/
英文:

The tomcat image you are using uses jdk8 as java version, but the war you are building uses java11. So tomcat is not deploying your app.
Just use an tomcat jdk11 image and your app should start working.
You can use this image.

FROM tomcat:9.0-jdk11

To reduce vulnerabilities, the webapps folder in docker image will be empty, but the required folders to manager and other operations will be stored in webapps.dist folder. So if you need the manager and homepage ui , just copy the items from webapps.dist to webapps directory.

RUN cp -r $CATALINA_HOME/webapps.dist/* $CATALINA_HOME/webapps/

huangapple
  • 本文由 发表于 2020年10月27日 01:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64541862.html
  • docker
  • java
  • spring-boot
  • spring-mvc
  • tomcat
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定