Spring Boot not serving static content
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is widely recognized for its ability to simplify the development of Java applications, particularly when it comes to creating stand-alone, production-grade Spring-based applications. One of its many features is its built-in ability to serve static content. Despite this, developers occasionally encounter scenarios where Spring Boot fails to serve static content as expected. This article explores the technical reasons behind such issues, examines common pitfalls, and provides solutions for addressing these problems.
Understanding Static Content in Spring Boot
By default, Spring Boot serves static content from the following locations in the classpath:
/static/public/resources/META-INF/resources
When a request is made for static content, Spring Boot searches these directories in the order listed and serves the first matching resource it finds.
Common Reasons for Static Content Not Being Served
1. Incorrect Directory Placement
One of the most common reasons Spring Boot does not serve static content is the placement of resources in incorrect directories. Static resources must be placed in one of the recognized locations mentioned above.
Solution: Verify that your resources (e.g., HTML, CSS, JavaScript files) are correctly placed in any of the default directories.
2. Configuration Issues
Spring Boot's auto-configuration can sometimes be overridden unintentionally in application.properties or application.yml.
Solution: Ensure that you haven't explicitly disabled static resources by configuring the spring.resources.static-locations property incorrectly. Here’s an example configuration in application.properties that sets a custom static resource location:
Ensure that the specified folder exists and contains the resources you intend to serve.
3. DispatcherServlet Mapping
Spring Boot's DispatcherServlet handles all requests, and if not configured properly, it may affect static resource serving.
Solution: Ensure that DispatcherServlet mappings do not overlap with static resource paths. If necessary, reconfigure the mappings in such a way that they don't interfere with the default static resource handling.
4. Custom WebMvcConfigurerAdapter
Implementing WebMvcConfigurer (previously WebMvcConfigurerAdapter) can override default static content configurations.
Solution: When defining a custom WebMvcConfigurer, ensure static resource handlers are properly configured:
5. Absence of ResourceChain
By default, Spring Boot uses a ResourceChain for better resource handling. Disabling the resource chain might lead to fewer features being available.
Solution: Enable the resource chain if possible:
Handling Security Configurations
Security configurations can sometimes inadvertently block access to static resources. Specifically, Spring Security, if not configured correctly, might prevent static content from being served.
Solution: Override WebSecurityConfigurerAdapter to permit public access to static resources:
Troubleshooting Checklist
Below is a table summarizing the key points when troubleshooting why Spring Boot might not serve static content:
| Issue Type | Description | Solution |
| Incorrect Directory | Resources not in correct locations | Place resources in /static, /public, etc. |
| Configuration Override | Properties overriding default behavior | Check application.properties for misconfigs |
| DispatcherServlet | Misdirected requests due to servlet mapping | Ensure servlet mappings don't interfere |
| Custom WebMvcConfigurer | Overriding default static handling mechanisms | Properly configure resource handlers |
| Security Configuration | Spring Security blocking static resources | Configure security to allow access |
Additional Subtopics
Considerations for WebJars
WebJars provide an elegant solution for managing web libraries using JAR packaging. Spring Boot natively supports WebJars, allowing you to utilize resources from /webjars/**.
To ensure WebJars function correctly, verify they are included as dependencies in your pom.xml or build.gradle and correctly requested in your HTML files.
Custom Static Locations
In situations where you require custom static locations, extend the default static location strategy to include these new paths. This often involves a combination of property configuration and custom MVC configuration.
Monitoring and Logging
Utilize logging to monitor static resource requests and determine if they are being intercepted or mishandled. Configuring higher-level logging for debugging purposes can give insights into potential misconfigurations.
Spring Boot provides ample support for serving static content, but understanding its default behavior and configuration nuances is crucial for effective application development. By closely inspecting configurations, directory setups, and security settings, you can overcome obstacles related to serving static content in Spring Boot applications.
Related reading
- Spring Boot Oauth2 client credentials
- Spring boot on Kubernetes does not get restarted on java.lang.OutOfMemoryError Java heap space
- Spring Boot Overriding favicon
- Spring Boot Page Deserialization - PageImpl No constructor
- Spring Boot Primefaces - Unrecognized Content Type Exception
- Spring Boot Program cannot find main class
- Spring Boot project in IntelliJ community edition
- Spring Boot project shows the Login page

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.