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

suj*_*mar 0 java spring tomcat http-status-code-404

从 Eclipse 或 Intellij idea 运行 Spring Boot 应用程序时,它会在嵌入式 tomcat 服务器上运行。但是当部署在外部 tomcat 服务器上时,它会出现 404 错误。

Mah*_*oya 6

确保您已完成以下步骤:

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

 }
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  1. 将包装更新为战争

<packaging>war</packaging>

  • 将生成的war复制到Tomcat的webapp文件夹中,重新启动tomcat。
  • 转到 tomcat 的管理页面,看看您是否可以找到您的应用程序及其状态正在运行/启动。访问 URL 时,请确保您附加了正确的上下文路径,如果使用 application.properties 文件中的“server.context”属性定义。

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