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

Spring Boot - Project Deployment Using Tomcat

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:

1. Convert to WAR

By default, Spring Boot applications are packaged as JARs. For deployment to an external Tomcat, you need a WAR package.

In your pom.xml:

  • Change the packaging type to war:
<packaging>war</packaging>
  • Exclude the embedded Tomcat by marking it as provided:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

2. Extend 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.

3. Build the WAR

Use Maven or Gradle to build your project:

mvn clean package

This will generate a .war file in the target directory.

4. Deploy on Tomcat

  1. Copy the generated .war file to Tomcat's webapps directory.
  2. Start Tomcat. This can be done using startup.sh (or startup.bat on Windows) located in the bin directory of your Tomcat installation.
  3. Once Tomcat starts, it will automatically deploy the WAR and your application should be accessible at http://localhost:8080/your-war-name.

5. Context Path

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:

  • Rename your .war file. The context path will be the name of the WAR file without the .war extension.
  • OR Set the context path in Tomcat's server.xml by adding a Context element.

6. Tomcat Version

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.

Conclusion

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.

  1. Configuring Spring Boot for deployment on Tomcat:

    • Description: Spring Boot applications can be configured for deployment on the Apache Tomcat servlet container. This involves specifying the necessary dependencies and configurations.
    • Code:
      <!-- POM.xml configuration for Tomcat -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
      </dependency>
      
  2. WAR packaging and deployment with Spring Boot and Tomcat:

    • Description: Spring Boot applications can be packaged as WAR files for deployment on external Tomcat instances. This allows more flexibility in Tomcat version selection.
    • Code: Set the packaging type to WAR in the pom.xml:
      <packaging>war</packaging>
      
  3. Externalizing Tomcat configuration for Spring Boot applications:

    • Description: Externalizing Tomcat configuration allows overriding default settings. This is useful for adjusting Tomcat-specific properties in a Spring Boot application.
    • Code:
      # Externalized Tomcat configuration in application.properties
      server.tomcat.uri-encoding=UTF-8
      
  4. Enabling SSL and HTTPS for Spring Boot on Tomcat:

    • Description: Enable SSL and configure HTTPS for a Spring Boot application deployed on Tomcat to secure communication.
    • Code:
      # SSL configuration in application.properties
      server.port=8443
      server.ssl.key-store=classpath:keystore.jks
      server.ssl.key-store-password=secret
      
  5. Setting up context paths and virtual hosts in Spring Boot with Tomcat:

    • Description: Customize context paths and virtual hosts for Spring Boot applications on Tomcat to control the application's URL structure.
    • Code:
      # Context path configuration in application.properties
      server.servlet.context-path=/myapp
      
  6. Managing sessions and clustering with Spring Boot and Tomcat:

    • Description: Tomcat supports session management and clustering for scalability. Configure Spring Boot applications to use Tomcat's session handling.
    • Code:
      # Session clustering configuration in application.properties
      server.servlet.session.cookie.name=mySessionCookie
      server.servlet.session.timeout=1800