SpringBoot配置本地tomcat

springboot默认用的内嵌tomcat容器,我们想修改为本tomcat,打成war包怎么办呢,这就需要修改做以下配置了,不然启动404

  • 首先修改pom.xml配置,移除默认tomcat容器,默认jar修改为war
<packaging>war</packaging>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
		</exclusions>
	</dependency>
</dependencies>
  • 接下来修改项目启动类,重写SpringBootServletInitializer类的configure方法
package com.site.blog.my.core;

import org.mybatis.spring.annotation.MapperScan;
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;

@MapperScan("com.site.blog.my.core.dao")
@SpringBootApplication
public class MyBlogApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MyBlogApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MyBlogApplication.class);
    }
}

小结

  • 个人猜测是项目未能加载到项目配置文件,导致项目启动无法访问出现404,欢迎交流