首页 > 解决方案 > WAR 文件已部署,但在尝试获取数据时响应 404

问题描述

在 Tomcat 10 上,当部署 war 文件时,tomcat 会进行部署,但在访问它时说资源不可用,并以 404 状态代码响应。Tomcat 日志说没有找到该 jar 的 TLD。我能够运行war文件java -jar ./Game-0.0.1-SNAPSHOT.war并在浏览器中访问它。

环境:运行 CentOS、Tomcat 10、Java 11。

我已将依赖项和打包添加到我的 pom.xml 中,并编辑了主类。

pom.xml:

<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>Game</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Game</name>
<packaging>war</packaging>
<description>Game</description>

<properties>
    <java.version>11</java.version>
    <start-class>game.GameApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.20.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.16.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.12.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.12.3</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

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

游戏应用程序.java

package game;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class GameApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(GameApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(GameApplication.class);
    }

}

Tomcat 日志

05-Aug-2021 16:07:10.780 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-10.0.8/webapps/host-manager] has finished in [49] ms
05-Aug-2021 16:07:10.781 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-10.0.8/webapps/manager]
05-Aug-2021 16:07:10.814 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-10.0.8/webapps/manager] has finished in [29] ms
05-Aug-2021 16:07:10.814 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-10.0.8/webapps/ROOT]
05-Aug-2021 16:07:10.838 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-10.0.8/webapps/ROOT] has finished in [22] ms
05-Aug-2021 16:07:10.841 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
05-Aug-2021 16:07:10.852 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [2988] milliseconds
05-Aug-2021 16:07:24.530 INFO [http-nio-8080-exec-4] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/Game-0.0.1-SNAPSHOT] has started
05-Aug-2021 16:07:26.251 INFO [http-nio-8080-exec-4] 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.
05-Aug-2021 16:07:26.257 INFO [http-nio-8080-exec-4] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/Game-0.0.1-SNAPSHOT] is completed

我尝试过手动部署,并通过 tomcat manager gui 进行部署,这是显示在 tomcat manager gui 中部署的 war 文件的屏幕截图。 雄猫管理器

标签: javaspring-bootmavenwebtomcat

解决方案


推荐阅读