Spring Boot
static resources
src/main/resources
Java
troubleshooting

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.

Browse interview questions

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/static
  • src/main/resources/public
  • src/main/resources/resources
  • src/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:

 
1my-spring-boot-app/
23├── src/
4│   └── main/
5│       ├── java/
6│       └── resources/
7│           ├── static/
8│           │   ├── css/
9│           │   │   └── style.css
10│           │   ├── js/
11│           │   │   └── script.js
12│           │   └── images/
13│           │       └── logo.png
14│           ├── application.properties
15│           └── templates/
16├── pom.xml

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:

java
1import org.springframework.context.annotation.Configuration;
2import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
3import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4
5@Configuration
6public class WebConfig implements WebMvcConfigurer {
7    @Override
8    public void addResourceHandlers(ResourceHandlerRegistry registry) {
9        registry.addResourceHandler("/assets/**")
10                .addResourceLocations("classpath:/assets/");
11    }
12}

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:

properties
spring.resources.static-locations=classpath:/custom-static/

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:

AspectBest Practice
Directory PlacementUse directories: static, public, resources, META-INF/resources
Custom LocationsConfigure via WebMvcConfigurer if necessary
Build PathEnsure src/main/resources is on the build path
Property ConfigurationsVerify 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.