SpringBoot is packaged as a war package and runs in tomcat

Keywords: Java Tomcat Spring SpringBoot xml

Let's take a look at the simplest project I've created.

  • controller class
@RestController
public class TestController {
    @RequestMapping("index")
    public String index() {
        return "hello";
    }
}
  • The pom file hasn't been modified, so let's start the project directly. Look at the version of embedded tomcat in the startup log.

My version here is 8.5.37, which is determined by the version of spring boot that you chose. My version of spring boot is 2.0.8. Okay, we're going to visit the project directly. The visit was successful.

Okay, we just started this class directly in idea, and of course we can pack it into a jar package to start it, so instead of demonstrating it, we can just start packing it into a war package.

1. Modify the pom file, because springboot uses the built-in tomcat, so we do the following

<!-- Change the packing method to war package-->
    <packaging>war</packaging> 

<!--Exclusion of bone inlay tomcat,Modify the first dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- Remove Embedded tomcat Plug-in unit -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- We removed the embedded tomcat,You need to add the corresponding tomcat Dependency package -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

OK, pom.xml is finished. At this point, when we start SpringBootApplication.java directly in idea, we will report the following error

2. Create a new startup class. tomcat can't find the springboot startup class. We need to create a new class to point to it. This class inherits the SpringBootServletInitializer class and overrides the configure method.

Create a class under the same spring boot startup class

/**
 * Modify the startup class, inherit SpringBootServletInitializer, and override the configuration method
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // Note that this points to the Application startup class that was originally executed with the main method
      return builder.sources(SpringbootWardemoApplication.class);
    }
}

Through the above two steps, the process from jar package to war has been completed.

Because the packaging method is war package, all startups of this class will be wrong, and similarly, can not be packaged as jar package. Then we configure the tomcat boot mode in idea, and the tomcat version will be the same as the previous embedded version (theoretically, as long as the large version is the same, the same as tomcat 8.5). For more tomcat versions, see tomcat versions downloaded
There may be an episode when tomcat is configured in idea to boot, that is, you can not find the corresponding war package. You can use maven to package it first, or click here.

After we started with tomcat in idea, there was no problem.

### Finally, we wrapped it in maven and started it in our tomcat.

  • Attention should be paid to the following points:

    1. Some of the previous configurations in spring boot did not work, and the most obvious one was the configuration of ports.

    2. How to modify the previous version of springboot project is as long as it is modified at the beginning of pom.xml.

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.0.8.RELEASE</version>
          <relativePath/>  
      </parent>

    3.How to view the corresponding embedded tomcat version in your sprinboot

Posted by robpoe on Sun, 13 Oct 2019 05:41:56 -0700