将springboot项目部署到服务器的tomcat中无法访问


第一步:让启动类继承SpringBootServletInitializer,并重写configure方法,关键代码如下

@SpringBootApplication
public class MySpringBootApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);

    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

第二步:在pom文件中添加打包方式

war

第三步:在pom文件中去除自带的tomcat


            org.springframework.boot
            spring-boot-starter-web
            
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
        
            javax.servlet
            javax.servlet-api
            provided
        

第四步:在pom.xml文件中的maven插件中指定打包后的名字finalName

    
        demo
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

注意:打包后的名字要与虚拟路径一致。如application.properties中配置了server.servlet.context-path=/demo,则finalName中指定为demo.