【问题标题】:When Spring Boot app is deployed on tomcat gives 404 error当 Spring Boot 应用程序部署在 tomcat 上时出现 404 错误
【发布时间】:2018-08-24 14:19:27
【问题描述】:

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

【问题讨论】:

    标签: java spring tomcat http-status-code-404


    【解决方案1】:

    确保您已完成以下步骤:

    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. 更新包装为战争

    &lt;packaging&gt;war&lt;/packaging&gt;

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

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

    【讨论】:

    • 感谢@mahesh_Loya 的解决方案。
    • 嗨@Mahesch_Loya 感谢您的解决方案。我的 Spring Boot 应用程序部署在 tomcat /attributes 目录中。我不明白是否需要在 application.properties 中设置 server.context 属性?我的控制器正在等待 @RequestMapping("/"),当我转到 servername/attributes 时,我收到 404。应用程序在本地响应 localhost:8080 时运行良好。我的问题是我的 application.properties 中是否需要 server.servlet.context-path=/attributes。
    • @Ana Sustic 您可以分享您的示例项目的 git 链接吗?我会检查并更新。
    • 嗨@Mahesh_Loya 感谢您的提议。我解决了。我的终点不正确。
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2021-12-18
    • 2023-03-04
    • 2021-05-14
    相关资源
    最近更新 更多