1. Modify the packaging form
Set in pom.xml
<packaging>war</packaging>
2. Remove the embedded tomcat plugin
Find the dependency node in pom.xml and
spring-boot-starter-web
add the following code to it,
<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>
3. Add servlet-api dependency
Either of the following two methods can be used
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
Fourth, modify the startup class and rewrite the initialization method
We usually use the main method to start, there is an App startup class, the code is as follows:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
We need a configuration similar to web.xml to start the spring context. Add a SpringBootStartApplication class at the same level as the Application class. The code is as follows:
/**
* 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
*/
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(Application.class);
}
}
Five, package deployment
In the project root directory (that is, the directory containing pom.xml), enter: on the command line
mvn clean package
, wait for the packaging to complete, and[INFO] BUILD SUCCESS
the packaging is successful.
Then put the war package in the target directory into the webapps directory of tomcat, start tomcat, and it can be automatically decompressed and deployed.
Finally type in the browser
http://localhost:[端口号]/[打包项目名]/
Published successfully
Article source: http://blog.csdn.net/yalishadaa/article/details/70037846