Spring Boot access static resources missing scr/main/resources
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot provides a robust framework to create stand-alone, production-ready applications. One of its powerful features is the ease with which you can serve static content. However, developers might encounter issues when certain static resources such as images, CSS, or JavaScript files are missing or not served properly from the src/main/resources directory. This article explores how to correctly configure Spring Boot to serve static resources and troubleshoot common issues.
Understanding the Default Configuration
Spring Boot, by default, follows a particular convention for serving static resources. It looks for static content in the following directories within the classpath:
src/main/resources/staticsrc/main/resources/publicsrc/main/resources/resourcessrc/main/resources/META-INF/resources
These directories are searched in the order listed above, and the first match found is used.
Example File Structure
Here's how a typical Spring Boot project that serves static resources might look:
The static folder contains resources that should be directly accessible via HTTP. For instance, style.css would typically be accessed via http://localhost:8080/css/style.css.
Troubleshooting Missing Resources
Check Directory Structure
Ensure your static files are placed in one of the recognized directories (static, public, resources, or META-INF/resources). If files are in an unconventional location like src/main/resources/assets, they will not be served unless explicitly configured.
Verify Build Path
Make sure that your resources are in the build path. Check your build configuration (pom.xml for Maven or build.gradle for Gradle) to ensure that src/main/resources/ is included as a resource directory.
Custom Resource Locations
If you need to serve static resources from a non-standard location, you can customize the WebMvcConfigurer:
The above configuration serves resources from src/main/resources/assets accessible at /assets/.
Check Application Properties
Ensure there are no configurations in application.properties or application.yml that might override default settings. For example, the spring.resources.static-locations property can be set to define custom locations:
This setting changes the locations from which static files are served, which can be handy but may lead to resources being inadvertently missed.
Summary
Having static resources properly configured in a Spring Boot application is critical for the smooth operation of the front end. Here is a quick summary:
| Aspect | Best Practice |
| Directory Placement | Use directories: static, public, resources, META-INF/resources |
| Custom Locations | Configure via WebMvcConfigurer if necessary |
| Build Path | Ensure src/main/resources is on the build path |
| Property Configurations | Verify spring.resources.static-locations settings |
Conclusion
While Spring Boot's default convention is convenient for many use cases, knowing how to configure and troubleshoot issues with static resources is essential for adapting to more complex requirements. Ensure your resources are in the expected directories and understand how custom configurations can affect their accessibility.
By following best practices and knowing where potential issues might arise, you can prevent and resolve missing static resource problems effectively in your Spring Boot applications.
Related reading
- Spring Boot Actuator - LDAP Health Endpoint Throwing NPE
- Spring Boot Actuator / Swagger
- Spring Boot Actuator application won't start on Ubuntu VPS
- Spring boot actuator /health is not working
- Spring Boot Actuator Health Returning DOWN
- Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start
- Spring Boot Actuator hides property values in env endpoint
- Spring Boot Actuator to run in separate thread pool

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.