War package and jar package are common packaging methods. The concept of war package partial application can include front-end pages, publishing applications and interfaces; jar package partial service concept, usually in the form of jar package in the microservice scenario.
This section mainly introduces the process of SpingBoot hitting war package.
1 SpringBoot 4 steps to hit war package
- Set the packaging mode, the default is jar mode.
<!--war包[1] 默认是jar包形式-->
<packaging>war</packaging>
- Remove the built-in tomcat dependency. Because the war package is to be published to an external servlet container, the built-in Tomcat of the springboot web module is no longer needed.
<!--引入springboot web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--打war包需要移除springboot web模块中的内置tomcat依赖-->
<exclusions>
<exclusion>
<groupId>spring-boot-starter-tomcat</groupId>
<artifactId>org.springframework.boot</artifactId>
</exclusion>
</exclusions>
</dependency>
- Set servlet dependency
<!--打war包需要添加外部servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
- Set the war package application startup class
public class WarStarterApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 指向SpringBoot启动类--Application类
return builder.sources(Application.class);
}
}
At this point, we can find the generated jar package in the target directory of the project module that sets the war packaging method.
2 Publish the war package
Put the war package in the Tomcat/webapps directory, and the running Tomcat will automatically decompress the war package into a project, and you can request and access it at this time.
Note that the project name is the name of the project folder.
http://192.168.233.130:8088/foodie-api-1.0-SNAPSHOT/hello
协议://ip地址:端口号/webapps中项目目录名/路由