Spring Boot Serving Static Web Content and Resources in MVC

Spring Boot is used in almost every app running in production . Spring boot handles static content and resources in a bit different way . Almost every application has static resources like html , CSS and JavaScript .

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 application.Properties
  • 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

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

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 src/main/resources .

Now why src/main/resources ?

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)

Also Read

  1. Learn Spring Boot For Beginners
  2. Load File from Classpath Spring Boot
  3. 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 application.properties or application.yml.

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

spring boot static resources

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

Lets create a class ResourcesConfig which extends WebMvcConfigurer .

package com.frugalis.Spring.Boot.Resources;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ResourceConfigs implements WebMvcConfigurer
{
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
    {
        "classpath:/META-INF/resources/",
		 "classpath:/resources/",
        "classpath:/static/", 
		"classpath:/public/",
		"classpath:/custom/",
		"file:/opt/myfiles/"
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/**")
		.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)
		.setCachePeriod(3000);
    }
}

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 addResourceLocations .

How to Serve static Content From Filesystem

We can also serve static content from file system in spring applications using this method .

"file:/opt/myfiles/"

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 .

setCachePeriod(3000);

Do you want to Know More about Resource Handling in Spring Boot , Click here .

Running Application

We are now going to run this application as Spring Boot as Below .

run spring boot

Here is how the css looks like if we try to access from browser

spring boot static resources css

Here is the actual site with static resources loaded , a pretty basic header tag with css loaded from custom folder .

spring boot static resources example

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 .

Leave a Comment