Posted In: Spring, Spring Boot, Spring MVC

How to handle static content in Spring MVC and Spring Boot

How to exclude static content from being served by Dispatcher Servlet?

Spring MVC

1. All requests are set to pass through Dispatcher servlet



2. Create sample app.js file in /WebContent/resources folder. For Maven create JS file in /src/main/webapp/resources

3. Now try accessing following URL

Application will throw 404 error

WARNING: No mapping found for HTTP request with URI [/example/resources/app.js] in DispatcherServlet

4a. To resolve this now add following entry to your spring XML

<mvc:resources mapping="/resources/**" location="/resources/" />

Now try to access JS file. It should be accessible.

4b. Another way is to use Java Configuration

public void addResourceHandlers(ResourceHandlerRegistry registry) {

Spring Boot

Spring Boot automatically excludes following folders and serves them as web content


This is achieved through following code in org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
		"classpath:/META-INF/resources/", "classpath:/resources/",
		"classpath:/static/", "classpath:/public/" };

public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!registry.hasMappingForPattern("/webjars/**")) {
	if (!registry.hasMappingForPattern("/**")) {

by , on July 11th, 2017

  • Categories