From jar to war
<packaging>jar</packaging>
If it is the above packaging method, the startup method is
mvn package java -jar target/mymodule-0.0.1-SNAPSHOT.jar
change to war
<?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"> <!-- ... --> <packaging>war</packaging> <!-- ... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- ... --> </dependencies> </project>
Just change the scope of tomcat to provided
If you want to publish to an external tomcat and you need to change the startup method
Added ServletInitializer class
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
Application.class is the main startup class marked with @SpringBootApplication
Issues of attention
The name of the package typed at this time should be consistent with
server.context-path=/spring-boot in application.properties
If it is not the same, the context will change under the webapps published to tomcat