尝试部署简单的Hello Spring Boot应用程序时出现Tomcat 404错误

时间:2020-10-26 17:14:04

标签: java spring-boot docker spring-mvc tomcat

我认为我也有类似的问题,但不知道如何解决 Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

Error image

我正在尝试使用docker和Tomcat构建简单的hello world spring boot应用程序。我用maven制作了war文件,然后将其放在$ CATALINA_HOME / webapps /中,并通过执行[“ catalina.sh”,“ run”]启动服务器。但是我仍然无法访问我的应用。

Tomcat status image

错误:

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说我的应用程序正在运行

我试图再次发出请求以避免错误的映射 http:// localhost:8888 / demo / hello http:// localhost:8888 /你好

主类代码:

@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日志

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 个答案:

答案 0 :(得分:0)

您正在使用的tomcat图像使用jdk8作为Java版本,但是您正在建立的战争使用java11。因此,tomcat没有部署您的应用程序。 只需使用tomcat jdk11图像,您的应用就应该开始运行。
您可以使用此图像。

FROM tomcat:9.0-jdk11

为减少漏洞,docker映像中的webapps文件夹将为空,但管理人员和其他操作所需的文件夹将存储在webapps.dist文件夹中。因此,如果需要管理器和主页ui,只需将项目从webapps.dist复制到webapps目录。

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