繁体   English   中英

Spring Boot 应用程序在部署到 Tomcat 时提供 404,但适用于嵌入式服务器

[英]Spring Boot application gives 404 when deployed to Tomcat but works with embedded server

使用 Spring MVC 提供 Web 内容服务的指导下,我正在创建一个 Spring Boot Web 应用程序,我可以使用嵌入式 Tomcat 实例以及独立的 Tomcat 8 服务器运行它。

当作为java -jar adminpage.war执行时,应用程序按预期工作,当我访问http://localhost:8080/table时,我看到了预期的结果。 但是,当我部署到 Tomcat 8 服务器时(通过将adminpage.war放入webapps目录),我在访问https://myserver/adminpage/table时收到 404 错误。

catelina.loglocalhost.log文件对 Tomcat 服务器没有任何帮助。

谁能建议我在哪里配置错误? 我过去在使用 Spring Boot 部署 RESTful 服务时没有遇到过同样的问题,但这是我第一次涉足 Web 应用程序。

我的申请文件:

src/main/java/com/.../Application.java

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

}

src/main/java/com/.../MainController.java

@Controller
public class MainController {

    @RequestMapping("/table")
    public String greeting(Model model) {
        model.addAttribute("name", "Fooballs");
        return "table";
    }
}

src/main/resources/templates/table.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.foo</groupId>
  <artifactId>adminpage</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent>

  <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-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <build>
    <finalName>adminpage</finalName>

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

</project>

我忘记调整我的Application.java文件以扩展SpringBootServletInitializer并覆盖configure方法。

更正文件:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
  }

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

https://mtdevuk.com/2015/07/16/how-to-make-a-spring-boot-jar-into-a-war-to-deploy-on-tomcat/指出我的错误的提示.

在 Spring Boot 官方文档中创建可部署的战争文件的更多信息。

截至 2021 年 8 月。 如果有人在 Tomcat 10 服务器中遇到此问题,请阅读以下帖子,说明 Spring boot 在 Tomcat 10 中不起作用。 https://github.com/spring-projects/spring-boot/issues/22414

我遇到了这个问题并切换到以前的版本,它解决了这个问题。

如果有人在 IntelliJ 社区版中使用 sprint boot 时遇到同样的问题。 您只需要将主类放在主包(com.xyx)中,不要将其放在在 com.xyx 中创建的任何子包中。

将 Tomcat 从 10 版降级到 9 版对我有用。 2022 年 1 月更新,显然 tomcat 10 仍然不支持 spring-boot。 正如Dipu在这里提到的, https: //stackoverflow.com/a/68917349/13294956

只需在控制器注释中添加路由,如下所示:@RestController @RequestMapping(value = "/")

对我来说,Spring boot 不适用于 tomcat 10。我更改为 9 版本并且它有效。

引用节食者对另一个 SO 问题的回答

你的war文件名是priz-0.5.war。 所以上下文名称也是priz-0.5。 例如,您必须调用 http://localhost:8080/priz-0.5

如果你想像 http://localhost:8080/ 一样调用它,只需将 war-File 重命名为 ROOT.war

我有同样的问题,但我的 SpringBootConfiguration 一切都很好我终于找到了为什么我有一个 404

我刚刚从我的 pom.xml 中删除了 build->plugin 中的 maven-dependency-plugin

<!--
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
        </plugin>
-->

此删除使我最终能够成功地在 tomcat9 上测试我的 war 文件,并且仍然允许我通过 mvnw 进行测试

这可以帮助某人

我遇到了同样的问题,结果证明我已经为 java 11 编译了应用程序,而我的服务器运行的是 java 8。一旦我升级到 java 11,它就可以正常工作。 这很棘手,因为我在 Tomcats 日志中绝对没有任何内容表明该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM