Spring Boot Serving Static Web Content and Resources

FrugalisMinds

Author - FrugalisMinds

1. Introduction

Spring Boot is used in alomst 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

2. 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)

3. Custom Path to Serve static content

Now lets 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 .

We can add below properties in our application.properties or application.yml.

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

spring boot Resources

Always remember classpath above points to /src/main/resources folder , so put folders relative to this directory only .

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

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

4.2 Use of addResourceLocations

In scenarirs where we want to configure multiple resource locations in spring based application . We can use addResourceLocations .

4.3 How to Serve static Content From Filessystem

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

"file:/opt/myfiles/"

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

spring boot Resources

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

spring boot static content css

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

I hope You Enjoyed this Post with your favourite pizza topping 😇 . Please let us know in comments , so that we can reach you with better content .