Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Deploying a Spring Boot application using an external Tomcat instance (instead of the embedded server) involves a few steps. Here's a guide to help you deploy your Spring Boot application on Tomcat:
By default, Spring Boot applications are packaged as JARs. For deployment to an external Tomcat, you need a WAR package.
In your pom.xml
:
war
:<packaging>war</packaging>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
SpringBootServletInitializer
To make your application run in an external servlet container, you need to extend SpringBootServletInitializer
and override its configure
method:
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(YourSpringBootApplication.class); } }
Replace YourSpringBootApplication
with the name of your main Spring Boot application class.
Use Maven or Gradle to build your project:
mvn clean package
This will generate a .war
file in the target
directory.
.war
file to Tomcat's webapps
directory.startup.sh
(or startup.bat
on Windows) located in the bin
directory of your Tomcat installation.http://localhost:8080/your-war-name
.By default, your application will be available under the context path matching the name of your WAR file. If you want to set a custom context path:
.war
file. The context path will be the name of the WAR file without the .war
extension.server.xml
by adding a Context
element.Ensure that the version of Tomcat you're deploying to is compatible with the version of Spring Boot you're using. For instance, Spring Boot 2.x applications typically work well with Tomcat 9.
By following the steps mentioned above, you can successfully deploy your Spring Boot application to an external Tomcat server. This approach is particularly useful when you have existing infrastructure based on Tomcat or when you have multiple applications running on a single Tomcat instance.
Configuring Spring Boot for deployment on Tomcat:
<!-- POM.xml configuration for Tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
WAR packaging and deployment with Spring Boot and Tomcat:
pom.xml
:<packaging>war</packaging>
Externalizing Tomcat configuration for Spring Boot applications:
# Externalized Tomcat configuration in application.properties server.tomcat.uri-encoding=UTF-8
Enabling SSL and HTTPS for Spring Boot on Tomcat:
# SSL configuration in application.properties server.port=8443 server.ssl.key-store=classpath:keystore.jks server.ssl.key-store-password=secret
Setting up context paths and virtual hosts in Spring Boot with Tomcat:
# Context path configuration in application.properties server.servlet.context-path=/myapp
Managing sessions and clustering with Spring Boot and Tomcat:
# Session clustering configuration in application.properties server.servlet.session.cookie.name=mySessionCookie server.servlet.session.timeout=1800