Put the Spring-Boot project into a WAR package and publish it to the Tomcat container as usual

First, modify the packaging method

In pom.xml set war

Two, remove embedded Tomcat

In pom. The XML:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <! -- Remove embedded Tomcat plugin -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code

Third, add servlet-API dependencies

In pom. In the XML

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
Copy the code

Fourth, modify the start class, rewrite the initialization method

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

We need similar to the web. The XML configuration way to start the spring context, under the structure at the same level of Application class to add a SpringBootStartApplication class, the code is as follows:

/ * * * modify start class, inheritance SpringBootServletInitializer and rewrite the configure method * /
public class SpringBootStartApplication extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // Note that this refers to the Application launcher class that was originally executed with the main method
        returnbuilder.sources(Application.class); }}Copy the code

Five, package deployment

Double-click the Package in the MAVEN window of IDEA to package it. If successful, the WAR package will be generated in the target directory.

After the package is successfully packaged, you only need to put the WAR package into the Webapps in the Tomcat directory and run Tomcat again. The WAR package will be automatically decompressed and deployed.

Finally in the browser input, you can request access.

http://localhost:[port number]/[Package project name]/Copy the code