Spring Boot应用程序在部署到Tomcat时提供404,但与嵌入式服务器配合使用 [英] Spring Boot application gives 404 when deployed to Tomcat but works with embedded server

查看:87
本文介绍了Spring Boot应用程序在部署到Tomcat时提供404,但与嵌入式服务器配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Guided by Serving Web Content with Spring MVC, I'm creating a Spring Boot web application that I can run using both the embedded Tomcat instance as well as on a standalone Tomcat 8 server.

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

The application works as expected when executed as java -jar adminpage.war and I see the expected outcome when I visit http://localhost:8080/table. However, when I deploy to a Tomcat 8 server (by dropping adminpage.war into the webapps directory), I get a 404 error when I visit https://myserver/adminpage/table.

catelina.log localhost.log 文件在Tomcat服务器上没有任何帮助。

The catelina.log and localhost.log files contain nothing helpful on the Tomcat server.

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

Can anyone suggest where I've made a misconfiguration? I've not had the same problems in the past when deploying RESTful services with Spring Boot, but this is my first foray into a web application.

我的应用程序文件:

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 方法。

I had forgotten to tweak my Application.java file to extend SpringBootServletInitializer and override the configure method.

更正文件:

@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/ 指出我的错误。

Hat tip to https://mtdevuk.com/2015/07/16/how-to-make-a-spring-boot-jar-into-a-war-to-deploy-on-tomcat/ for pointing out my mistake.

创建一个可部署的war文件。

More info at Create a deployable war file in Spring Boot Official docs.

这篇关于Spring Boot应用程序在部署到Tomcat时提供404,但与嵌入式服务器配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆