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.
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.html – 4.html can be accessed via URLs like http://localhost:8080/1.html.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
