记录springboot打包war在tomcat启动,访问404,找不到资源。

1、pom.xml修改处

<modelVersion>4.0.0</modelVersion>
<groupId>com.xx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>xxx</name>
 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>

</dependency>


<!--spring-boot-starter-tomcat 是原来被传递过来的依赖,默认会打到包里,所以我们再次引入此依赖,并指定依赖范围为provided,这样tomcat 相关的jar就不会打包到war 里了.-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

2、Application.java修改处

在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册。
@ServletComponentScan

public class Application extends SpringBootServletInitializer

SpringBootServletInitializer
为了支持可以不使用web.xml。提供了ServletContainerInitializer,它可以通过SPI机制,当启动web容器的时候,会自动到添加的相应jar包下找到META-INF/services下以ServletContainerInitializer的全路径名称命名的文件,它的内容为ServletContainerInitializer实现类的全路径,将它们实例化

3、Idea工具打包路径 maven->package->Runmaven build

4、tomcat8.5
注:springboot2.0以上要求tomcat8以上,springboot要求jre环境为1.8以上