The SpringBoot project is packaged as a WAR and runs on Tomcat

Spring Boot is a new framework from the Pivotal team designed to simplify the initial setup and development process for new Spring applications.

  • The framework uses a specific way to configure so that developers no longer need to define boilerplate configurations.
  • In this way, Spring Boot aims to be a leader in the burgeoning field of rapid Application development.

SpringBoot runs on the basis of Tomcat, so how to package the SpringBoot project into a WAR and publish to the Tomcat server?

Maven introduces dependencies

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0. 0</modelVersion>

    <groupId>com.luo</groupId>
    <artifactId>SpringBootTest</artifactId>
    <version>1.0-SNAPSHOT</version>
     <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.57..RELEASE</version> <relativePath/> <! -- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.57..RELEASE</version> <! -- Exclude Tomcat from Spring or it will be displayed404Error --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <! Javax. servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> </dependency> </dependencies> </project>Copy the code

SpringBoot start class

@SpringBootApplication
@RestController
@ComponentScan
public class ShopApplication extends SpringBootServletInitializer {


    // Override configure to make Tomcat recognize the startup class
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ShopApplication.class);
    }

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

The Controller part

@Controller
public class MainController {

    @RequestMapping("/")
     public String index(a)
    {
        return "1"; }}Copy the code

Package the project into a WAR file using the MVN Clean Package

Tomcat is run successfully.