There are variety of ways to handle static content in spring boot . We will talk about these different ways below . How to Handle Static Content in Spring Boot .
There are variety of ways listed below
- Spring Default Way using
- customizing Spring static resources path
- Customizing Using Java Configurations
Spring Boot Default Way
Spring Boot automatically add static resources placed inside any of the following folders or directories in your class path
lets take an example i want to put my
style.css and access it . I need to create a folder named as static or public inside
This location is present under
classpath , hence everyone generally uses this location.
If we add
style.css as a file in my application . so put this static css in your spring boot application inside
static folder .
we can now access this static css file using
http://localhost:8080/style.css (Assuming server started on port 8080 and with root / as context)
- Learn Spring Boot For Beginners
- Load File from Classpath Spring Boot
- Constructor Injection and Field Injection
Custom Path to Serve static content
Now let’s say you want to put your css in a different folder from static folder . We can customize the spring boot resources location as well as shown below .Most of the times your spring boot application will be returning 404 , if the below settings and configurations are not completed .
We can add below properties in our
Always remember classpath above points to
/src/main/resources folder , so put folders relative to this directory only .
Customizing Spring Boot static resources Using Java Config
public class ResourceConfigs implements WebMvcConfigurer
private static final String CLASSPATH_RESOURCE_LOCATIONS =
public void addResourceHandlers(ResourceHandlerRegistry registry)
Now this provides you a more control to customize your configurations . Here are the important points here .
Use of addResourceHandler
We have used addResourceHandler() which is a simpler version and can be used in a simple app where we need one folder as a path to refer all static resources .
Use of addResourceLocations
In scenarirs where we want to configure multiple resource locations in spring based application . We can use
We can also serve static content from file system in spring applications using this method .
How to Serve Static HTML in Spring Boot
We are going to take an example where we are serving static html file from our resources . Serving static html is almost a common use case which can be handled the similar way we have handled the plain files. But there is a difference
Caching Static Content in Spring Boot
We gain more control here as we can set caching time for our static resources in spring boot .
Do you want to Know More about Resource Handling in Spring Boot , Click here .
We are now going to run this application as Spring Boot as Below .
Here is how the css looks like if we try to access from browser
Here is the actual site with static resources loaded , a pretty basic header tag with css loaded from custom folder .
I hope You Enjoyed this Post with your favorite pizza topping 😇 . Please let us know in comments , so that we can reach you with better content .