How to Serve Static Resources in Spring Boot: From Basic to Custom Configurations

This tutorial explains how Spring Boot handles static resources, covering the default classpath directories, a simple manual file‑stream approach, and ways to customize resource locations using Java configuration or application.yml, with code examples and practical URLs.

Programmer DD
Programmer DD
Programmer DD
How to Serve Static Resources in Spring Boot: From Basic to Custom Configurations

1. The simplest (but inefficient) approach

We can create a controller that reads a file from the resources/html directory and writes its bytes to the response. The code below demonstrates this method, which works for text files but is not suitable for production.

@Controller
public class StaticResourceController {

    @RequestMapping("/static/**")
    public void getHtml(HttpServletRequest request, HttpServletResponse response) {
        String uri = request.getRequestURI();
        String[] arr = uri.split("static/");
        String resourceName = "index.html";
        if (arr.length > 1) {
            resourceName = arr[1];
        }
        String url = StaticResourceController.class.getResource("/").getPath() + "html/" + resourceName;
        try {
            FileReader reader = new FileReader(new File(url));
            BufferedReader br = new BufferedReader(reader);
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            response.getOutputStream().write(sb.toString().getBytes());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. Spring Boot’s default static resource locations

Spring Boot automatically serves files placed under any of the following classpath directories:

classpath:/public/

classpath:/resources/

classpath:/static/

classpath:/META-INF/resources/

After creating these directories under src/main/resources, files such as 1.html4.html can be accessed via URLs like http://localhost:8080/1.html.

Directory structure
Directory structure

3. Custom static resource directory

To serve resources from a custom folder, define a configuration class that registers a resource handler:

@Configuration
public class ImageMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/images/");
    }
}

Alternatively, the same mapping can be declared in application.yml:

spring:
  mvc:
    static-path-pattern: /image/**
  resources:
    static-locations: classpath:/images/

After placing an image (e.g., spring.jpg) in src/main/resources/images, it is reachable at http://localhost:8080/image/spring.jpg.

Image example
Image example

4. Summary

Spring Boot serves static resources from the four default locations listed above, with the lookup order giving priority to classpath:/META-INF/resources/. This order can be changed by redefining spring.resources.static-locations. Custom directories can be added either via a WebMvcConfigurer implementation or through application.yml.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootStatic Resources
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.