Java Spring Boot 应用程序在部署到 Tomcat 时给出 404,但可与嵌入式服务器一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39567434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:15:37  来源:igfitidea点击:

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

javamavenspring-mvctomcatspring-boot

提问by Duncan Jones

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.

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

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

该应用程序在执行时按预期工作,java -jar adminpage.war当我访问http://localhost:8080/table. 但是,当我部署到 Tomcat 8 服务器时(通过adminpage.war放入webapps目录),当我访问https://myserver/adminpage/table.

The catelina.logand localhost.logfiles contain nothing helpful on the Tomcat server.

catelina.loglocalhost.log文件包含在Tomcat服务器上没有什么帮助。

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.

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

My application files:

我的申请文件:

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

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

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

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

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>

采纳答案by Duncan Jones

I had forgotten to tweak my Application.javafile to extend SpringBootServletInitializerand override the configuremethod.

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

Corrected file:

更正的文件:

@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);
  }
}

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.

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

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

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

回答by Hanumant

In case anyone having same problem while using sprint boot in the IntelliJ community edition. You just need to put your main class in the main package (com.xyx) don't put it in any subpackage which is created inside com.xyx.

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