【问题标题】:Tomcat 404 when deploying a war file which otherwise works in IntelliJTomcat 404 部署在 IntelliJ 中正常工作的 war 文件时
【发布时间】:2020-06-12 04:32:21
【问题描述】:

我一直在尝试创建一个 WAR 文件并将其部署在 Tomcat 服务器上。如果我在 Tomcat 服务器上的 IntelliJ 内运行 WAR 文件,它就可以工作。

为了使事情尽可能简单,我按照本教程 (https://www.baeldung.com/spring-boot-war-tomcat-deploy) 进行操作,但即使那个教程也不起作用。部署 war 文件时,我仍然遇到相同的 404 错误。教程应用程序在 IntelliJ 中工作,就像我的主要项目一样。我还很新,尝试了很多东西都无济于事:

  • 尝试在 pom.xml 文件中设置 line war,清理 maven 并使用目标文件夹中生成的 war 文件用于 Tomcat 服务器部署,或者按照官方网站 (@987654322) 显式构建 war 工件@) 并尝试运行它。

  • 尝试了不同的 URL,但都抛出 404 错误(除了默认的 Tomcat 页面,如 /manager)

  • 在 application.properties 文件中设置不同的路径 (server.servlet.context-path=/test)

  • 尝试了另一台计算机

错误:

我可能会犯什么新手错误?

我的 pom:

<?xml version="1.0" encoding="UTF-8"?>

https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.5.发布 com.example tomcat-test2 0.0.1-快照 战争 tomcat-test2 Spring Boot 演示项目

<properties>
    <java.version>1.8</java.version>
</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>

我的控制器:

@RestController

公共类 TomcatController {

@GetMapping("/hello")
public Collection<String> sayHello() {
    return IntStream.range(0, 10)
            .mapToObj(i -> "Hello number " + i)
            .collect(Collectors.toList());
}

}

主要应用:

@SpringBootApplication 公共类 TomcatTest2Application 扩展 SpringBootServletInitializer {

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

}

提前致谢, 亚历克斯

【问题讨论】:

  • 404 表示您正在请求tomcat 找不到的资源,这里有一些代码示例。你能发布你的 pom.xml 和你的控制器类吗?
  • 用代码更新了答案。

标签: tomcat intellij-idea http-status-code-404 war


【解决方案1】:

我通过更改 tomcat 版本解决了类似的问题。我的战争文件在 Tomcat 10 上得到 404,我使用 Tomcat 9 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 2012-05-08
    • 1970-01-01
    • 2014-07-03
    • 2015-04-24
    • 2018-03-13
    • 2017-11-08
    • 2020-12-03
    相关资源
    最近更新 更多