Transfer the jar package of springboot to war and put it into Tomcat

1. Modify the POM file and change the packaging form to war

war

2. Remove the embedded Tomcat module, but we need to introduce it for the convenience of local testing, so the configuration is as follows

         <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>
		<!--引入tomcat依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
            <!--只在编译时有效-->
			<scope>provided</scope>
		</dependency>
		<!--添加tomcat-servelt-api依赖-->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>7.0.42</version>
            <!--只在编译时有效-->
			<scope>provided</scope>
		</dependency>

3. Modify the startup class and override the configure method

Create a servletinitializer class under the same level of the original startup class, and inherit org springframework. boot. web. servlet. support. Springbootservletinitializer and override the configure method

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        // 指向启动类class
        return application.sources(DemoApplication.class);
    }

}

In this way, it can be run directly in Tomcat

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>