Migrating a Spring Boot application from an embedded Tomcat server to an external one means switching the packaging from an executable JAR to a deployable WAR that the external server can load.
Key Points: • Change the packaging type from jar to war in pom.xml or build.gradle. • Exclude or mark the embedded Tomcat starter dependency as provided so it doesn't conflict with the external server's own Tomcat. • Make the main application class extend SpringBootServletInitializer and override configure() to register the app with the servlet container. • Build the WAR file and deploy it to the external server's webapps directory or through its management console. • Verify context-path and server-specific settings, since the external server may serve the app under a different path than the embedded one did.
Example: After changing packaging to war and extending SpringBootServletInitializer, running mvn package produces app.war, which is dropped into Tomcat's webapps folder and picked up automatically on server startup.
Code Example:
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Interview Tip: A concise interview answer is:
"I switch packaging from jar to war, mark the embedded Tomcat starter as provided so it doesn't clash with the external server, and extend SpringBootServletInitializer so the app registers correctly as a servlet context. Then I build the WAR and deploy it to the external Tomcat's webapps folder."