SpringBoot項目啓用本地Tomcat

一、修改pom.xml文件,移除tomcat插件,並添加servlet-api依賴java

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除嵌入式tomcat插件 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
</dependency>

二、修改SpringBoot啓動類 2.1 修改類定義,使其繼承自SpringBootServletInitializer 2.2 重寫configure方法 原代碼web

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

修改後spring

@SpringBootApplication
public class HomeApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意這裏要指向原先用main方法執行的Application啓動類
        return builder.sources(HomeApplication.class);
    }

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

}

三、在IDEA中edit run configuration配置本地Tomcat便可api

相關文章
相關標籤/搜索