springboot项目部署到tomcat启动失败404

感谢这位老兄:《Spring Boot项目部署到tomcat启动失败404

 

我的springboot是2.7.0版本,需要部署到外部的tomcat版本是8.5,部署失败表象:

IDEA控制台只打印tomcat启动日志,没有springboot加载日志,访问localhost:8080/根路径/接口地址 报404

我查了springboot 2.7.0的内置tomcat的版本是9.0,所以一开始tomcat部署失败,我还以为是tomcat版本太低的原因。

 

后面经大师指点,修改以下3个地方,部署成功:

 

1)修改pom.xml,把打包形式从jar改成war

<properties>
      <packaging>war</packaging>
      <java.version>1.8</java.version>
      <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

2)在pom中添加一条依赖,使用provided参数来达到在外部tomcat启动时屏蔽springboot自带的tomcat容器

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
</dependency>

3)修改启动类,继承SpringBootServletInitializer

public class SpringbootApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
    //重写configure方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootApplication.class);
    }
}

 

end.

posted on 2022-12-02 17:56  梦幻朵颜  阅读(1595)  评论(0编辑  收藏  举报