Spring Boot War deployed to Tomcat
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot is widely appreciated for its ability to simplify the Java development process. While it allows for the creation of stand-alone applications with embedded server support, there are scenarios where deploying a traditional WAR file to an external web server, such as Apache Tomcat, is preferred. This article explores how to deploy a Spring Boot application to Tomcat by packaging it as a WAR file, detailing the necessary configurations and steps involved in the process.
Spring Boot and WAR Files
Spring Boot typically favors executable JARs, which are easy to run using embedded application servers. However, deploying a WAR (Web Application Archive) is essential when integrating with existing infrastructure, using distributed architectures, or adhering to enterprise guidelines that involve managing separate web servers such as Tomcat.
Getting Started with Spring Boot WAR Configuration
- Convert a Spring Boot JAR Application to WARBy default, Spring Boot applications are built as JARs. Transforming them into WAR involves some modifications:
- Update
pom.xml: Change the packaging type and add necessary dependencies.
- Create a Deployment Descriptor: Include a
web.xml(optional but recommended) for defining servlets and context configurations. - Extend
SpringBootServletInitializerin the Application Class:
Configuring Tomcat for Deployment
Tomcat needs to be correctly configured to accept WAR deployments:
- Tomcat Directory Structure: Ensure your Tomcat installation has the following structure, which includes directories like
bin,conf,logs, andwebapps. - Deploy WAR to
webappsDirectory: Copy the WAR file to thewebappsfolder. Tomcat will automatically deploy and unpack the WAR once it starts. - Configure Server Ports and Settings: Update the
server.xmlorconf/context.xmlfiles to manage specific deployment configurations, such as exposed ports, security settings, or resource configurations.
Development Considerations and Best Practices
When deploying a Spring Boot WAR to Tomcat, keep in mind the following considerations:
- Environment-Specific Configurations: Properties files and server settings should be segregated using Spring Profiles, enabling different configurations for development, testing, and production.
- Logging Integration: Ensure that logging configurations are set up to reflect both standard output logging during local development and appropriate enterprise-standard logging in production.
- Resource Utilization: Analyze and tune the resources allocated to the Tomcat server to prevent resource bottlenecks from affecting application performance.
- Security: Regularly update Tomcat and other dependencies to mitigate security vulnerabilities. Employ HTTPS and consider configuring thread pools correctly to manage high load efficiently.
Example Directory Layout and Files
A simplified example structure of a Spring Boot WAR file and associated files:
Key Points Comparison Table
| Category | Spring Boot JAR | Spring Boot WAR |
| Deployment Server | Embedded server (Tomcat, Jetty) | External Tomcat server |
| Configuration File | application.properties/yaml | Requires web.xml for additional servlet configurations |
| Packaging | Stand-alone JAR | WAR |
| Portability | Highly portable | Relies on the web server |
| Use Cases | Microservices, serverless applications 🍃 | Traditional web applications, controlled environments |
Conclusion
Deploying a Spring Boot application as a WAR file to a Tomcat server allows for integration into existing enterprise environments and provides additional control over server configurations. While it introduces additional complexity compared to Spring Boot JAR deployment, the trade-offs in terms of management, control, and scalability make it suitable for various enterprise scenarios. Ensure thorough configuration management and resource allocation to maximize application performance and security.
Related reading
- Spring RestTemplate - how to enable full debugging/logging of requests/responses?
- Springboot Wildfly 10 deployment error jdk.unsupported module not found
- SpringBoot with LogBack creating LOG_PATH_IS_UNDEFINED folder
- SSH into Kubernetes cluster running on Amazon
- Spring boot Webclient's retrieve vs exchange
- Spring Boot Websockets in Wildfly
- ssh remote host identification has changed
- SSH to Elastic Beanstalk instance

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.