当 Spring 启动应用程序部署在 tomcat 上时出现 404 错误

When Spring Boot app is deployed on tomcat gives 404 error

Spring 当 运行 来自 Eclipse 或 intellij idea 时,在嵌入式 tomcat 服务器上启动应用程序 运行s。但是当部署在外部 tomcat 服务器上时,它会出现 404 错误。

确保您已完成以下步骤:

  1. 扩展 SpringBootServletInitializer
@SpringBootApplication
 public class SpringBootWebApplication extends SpringBootServletInitializer 
{

@Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
    return application.sources(SpringBootWebApplication.class);
   }

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

 }
  1. 标记为您提供的嵌入式servlet容器pom.xml
<!-- marked the embedded servlet container as provided -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
  </dependency>
  1. 将包装更新为 war

<packaging>war</packaging>

  • 将生成的 war 复制到 Tomcat`s webapp 文件夹和 re-start tomcat.
  • 转到 tomcat 的管理页面,看看您是否可以找到您的应用程序,它的状态是 running/started。在访问 URL 时,请确保您附加了正确的上下文路径,如果在 application.properties 文件中使用 "server.context" 属性 定义。

如果您仍然遇到问题,请粘贴任何具体错误以防万一。