spring-boot
java
web-development
static-files
css

Where to put static files such as CSS in a spring-boot project?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In a Spring Boot project, correctly placing static files such as CSS, JavaScript, and images is critical for efficient web application development. This not only organizes the project structure but also ensures that the application serves static resources properly. Here’s how you can set it up effectively.

Static Files in Spring Boot Projects

Spring Boot aims to simplify the process of building stand-alone, production-grade Spring-based applications. One of the critical aspects of any web application is serving static content. Spring Boot provides a standard location for static resources, making it easy to manage and serve these files.

Default Locations

Spring Boot serves static content from specific directories in the classpath. By default, it looks for resources in the following directories:

  • /static
  • /public
  • /resources
  • /META-INF/resources

These directories should be placed under the src/main/resources directory. When your application is packaged, these resources are packed into the JAR file and can be accessible via the web.

Example Structure:

 
1my-spring-boot-project/
2  ├── src/
3  │   ├── main/
4  │   │   ├── java/
5  │   │   ├── resources/
6  │   │   │   ├── static/
7  │   │   │   │   ├── css/
8  │   │   │   │   ├── js/
9  │   │   │   │   └── images/
10  │   │   │   ├── templates/
11  │   │   └── ...
12  └── ...

Serving Static Files

Spring Boot's ResourceHandlerRegistry automatically maps these paths to the root path of your web context. For instance, a file located at src/main/resources/static/css/style.css will be available at http://<server-host>:<port>/css/style.css.

Customizing Static Locations

You may override the default locations and define your own by using the addResourceHandlers method in a @Configuration class. Implement the WebMvcConfigurer interface for fine-grained control.

Example:

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
8    @Override
9    public void addResourceHandlers(ResourceHandlerRegistry registry) {
10        registry.addResourceHandler("/static/**")
11                .addResourceLocations("classpath:/custom-static/");
12    }
13}

In this setup, you can put your static files under src/main/resources/custom-static/.

CSS, JS, and Image Files Organization

Depending on the size and complexity of your project, organizing static files into different folders such as css, js, and images is a standard practice. This not only helps with direct access but also enhances load time through browser caching and better file management.

Example Files:

 
1src/main/resources/static/
2    ├── css/
3    │   └── style.css
4    ├── js/
5    │   └── app.js
6    └── images/
7        └── logo.png

Caching Static Files

Spring Boot can cache static content to improve the performance of your web application. You can define cache policies by setting the Cache-Control HTTP header.

Example:

java
registry.addResourceHandler("/css/**")
        .addResourceLocations("classpath:/static/css/")
        .setCachePeriod(3600);

This example configures the application to cache CSS files for 3600 seconds (1 hour).

Serving Static Files in Production

In production environments, it's advisable to serve static content directly via a content delivery network (CDN) or a dedicated static file server, such as Nginx or Apache, to reduce load and improve page load times. However, Spring Boot’s setup is more than adequate for development and small-to-medium-sized projects.

Summary Table

AspectDefault BehaviorCustomization
Default Directories/static, /public, /resources, /META-INF/resourcesUse addResourceHandlers to define custom paths
Access URLhttp://<host>:<port>/<folder>Same, can be customized via handler
Organizing FilesUse separate folders for CSS, JS, Images under /staticFollow the same practice for clarity
CachingNot enabled by defaultSet via setCachePeriod on handler
ProductionServed from the same app serverUse a CDN or a dedicated file server

This structure and configuration ensure that your static files are efficiently organized and accessible, forming a solid foundation for your Spring Boot application. By understanding these various aspects, you can create a robust setup, enabling both development efficiency and production performance.


Course illustration
Course illustration

All Rights Reserved.