SpringBoot packaged as war package, and run in the tomcat

Look at the most simple project I created it.

  • controller class
@RestController
public class TestController {
    @RequestMapping("index")
    public String index() {
        return "hello";
    }
}
  • pom file does not have to make any changes, we directly start the project, pay attention to the log in to see the start of an embedded version of the tomcat
    file

Here is my version 8.5.37, this is the version you selected springboot a given, I use the springboot is 2.0.8 version. Well, we have direct access to the project, and a successful visit

file

Well, we have just started directly from the idea that in this class, of course, can be packaged into a jar package started, do not demonstrate a direct start packing for the war package

1. Modify the pom file because the embedded tomcat springboot use, so we do the following

<!-- 将打包方式改为war包-->
    <packaging>war</packaging> 

<!--排除骨嵌式tomcat,修改第一个依赖-->
        <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,就要添加对应的tomcat依赖包 -->
        <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>

Well, pom.xml be changed over, this time, will be reported the following error when we start directly in the idea of ​​SpringBootApplication.java
file

2. Create a startup class, tomcat is starting classes can not find springboot, we want to create a new class to it, this class should inherit SpringBootServletInitializer class and override the configure method

Creating a class in the same class at our springboot start

/**
 * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
      return builder.sources(SpringbootWardemoApplication.class);
    }
}

Through the above two steps, the process has been completed by the jar package into the war.

Because a packaging bag for the war, all classes will start this error, the same can not be packaged as a jar package. Then we configure tomcat start-up mode, and prior to the version of tomcat as embedded in the idea of (theoretically as long as the same like a big version, the same as tomcat8.5). More tomcat version download, see tomcat download each version
in the configuration tomcat startup idea might be a little episode, I could not find the corresponding war package, you can first use maven package once, or click here next
file

After the idea we started with tomcat way, no problem.

file

### Finally, we use maven package, and then put our tomcat start on it

  • Note the following:

    1. Before some of the configuration does not take effect in springboot, the most obvious is the configuration port

    2. how to modify the previous version of springboot project, as long as the place in pom.xml beginning of the modification on it

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

    3. How can I check myself sprinboot corresponding tomcat Embedded

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/11667978.html