Springboot项目打war包部署到tomcat上,tomcat正常启动,访问报错404

问题描述;Springboot项目打成war包,部署到tomcat上,正常启动(tomcat控制台没有报错!)访问报错404

     //参考了别人的博客,自己做个记录

    1,需要创建一个初始化类,两种方式:

             我的启动类是:DemoTomcatApplication,

             方式一:启动类直接继承:SpringBootServletInitializer,重写方法:configure

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication  extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }

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


}

                   方式二:重新创建一个配置类实现类,放在启动类同样的位置,sources方法的参数是启动类class

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class DemoServletApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);//注意:参数是启动类.class,不是当前类
    }
}

  2,修改pom文件,排除web启动器中的tomcat,防止和tomcat服务器起冲突

             方式一:直接在依赖spring-boot-starter-web中排除

        
            org.springframework.boot
            spring-boot-starter-web
            
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        

           方式二:修改依赖

        
            org.springframework.boot
            spring-boot-starter-tomcat
            
            provided
        

       3,jar访问不需要项目名:IP地址:端口号/url路径,war访问需要项目名:IP地址:端口号:项目名/url路径

            http://localhost:8080/demo-0.0.1-SNAPSHOT/test/show  ,我没有修改war包名称为项目名,所以直接用war包名访问。

你可能感兴趣的:(spingboot,spring,boot)