How can I serve static html from spring boot?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Serving static HTML content from a Spring Boot application is a common requirement for many web developers. Spring Boot, with its embedded container and minimal configuration, simplifies the process of deploying a standalone application. This article provides a comprehensive guide on how to serve static HTML files using Spring Boot, delving into configurations and practical examples.
Basic Setup
Before diving into serving static HTML, ensure that you have a Spring Boot project set up. If not, you can create a new project using the Spring Initializr website or through an IDE like IntelliJ IDEA or Eclipse.
Creating a Spring Boot Project
To create a Spring Boot project using Spring Initializr, follow these steps:
- Visit Spring Initializr: Go to start.spring.io.
- Project Settings:
- Choose a project type (
MavenorGradle). - Select the language (
Java,Kotlin, orGroovy). - Pick a Spring Boot version.
- Project Metadata:
- Group:
com.example - Artifact:
static-content-server - Packaging:
Jar - Java version:
11or higher
- Add Dependencies: Include
Spring Web. - Generate and Download: Click on "Generate" to download the project.
Directory Structure for Static Content
Spring Boot looks for static content in specific locations within the src/main/resources folder by default. These locations include:
staticpublicresourcesMETA-INF/resources
Directory Example
For this instance, consider creating a static folder. Your project structure should look like this:
Serving HTML Files
Any HTML file placed in the static directory will be available at http://localhost:8080/<filename>. For instance, an index.html file will be served when accessing http://localhost:8080/.
Customizing the Path
If you need to serve static content from a different directory or customize URL paths, adjust the application.properties file.
Example Configuration
After changing this configuration, ensure your HTML files are placed in the custom-static-folder.
Advanced Customization
Serving at the Root Path
If you want to set a different file to be served at the root path, e.g., a homepage, create a WebMvcConfigurer to customize the paths:
Setting Cache Control
It's often useful to define cache control headers for static resources to improve performance.
Summary Table
| Feature | Description | Default Path/Setting |
| Default Static Locations | Directory paths for serving static content. | static, public, resources, META-INF/resources |
| Custom Static Location | Specify a custom directory for static resources. | spring.web.resources.static-locations |
| Root URL Configuration | Serve specific HTML at root path. | Forwarding via WebMvcConfigurer |
| Cache Control | Improve performance with caching. | Define using ResourceHandlerRegistry |
Conclusion
Serving static HTML in Spring Boot is straightforward due to its predefined configurations and conventions. By understanding the default setup and knowing how to customize paths and caching, you can effectively manage and serve static content within your applications. Remember to leverage Spring Boot's flexibility for optimizing performance and organizing your resources efficiently.

