How can I serve static html from spring boot?

The json url works fine, but when I try to access localhost:8080/tw I get a blank page, and this error in the console:

2017-02-22 15:37:22.076 ERROR 21494 --- [nio-8080-exec-9] o.s.boot.web.support.ErrorPageFilter : Cannot forward to error page for request [/tw] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false 
Am I doing something wrong? 683 1 1 gold badge 6 6 silver badges 14 14 bronze badges asked Feb 22, 2017 at 13:44 1,054 1 1 gold badge 9 9 silver badges 17 17 bronze badges you don't really need tomcat dependency, spring starter web & thymeleaf should do it. Commented Nov 24, 2017 at 6:21

thymeloaf is not needed. Just put index.html in src/main/resources/static/ folder and static html is done!

Commented Nov 5, 2020 at 14:42

8 Answers 8

Static files should be served from resources, not from a controller.

Spring Boot will automatically add static web resources located within any of the following directories:

/META-INF/resources/ /resources/ /static/ /public/ 
answered Feb 22, 2017 at 13:49 4,024 2 2 gold badges 22 22 silver badges 32 32 bronze badges

oh, ok so I saw that localhost:8080/index2.html is viewable from the browser actually, and there is no way to serve it from the controller ?

Commented Feb 22, 2017 at 13:55

I have to add spring-boot-starter-thymeleaf and only refer to files in the templates directory, I think I got it, thank you

Commented Feb 22, 2017 at 14:58 thanks! I created the folders resources/static , put a index.html file and it just works! Commented Oct 22, 2017 at 0:21 where exactly should these directorys be? i hope src/main/resources is not served automatically Commented Dec 5, 2019 at 11:25 These dirs should be located in src/main/resources . src/main/resources is not served automatically. Commented Dec 5, 2019 at 13:27

In Spring boot, /META-INF/resources/ , /resources/ , static/ and public/ directories are available to serve static contents.

So you can create a static/ or public/ directory under resources/ directory and put your static contents there. And they will be accessible by: http://localhost:8080/your-file.ext . (assuming the server.port is 8080)

You can customize these directories using spring.resources.static-locations in the application.properties .

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

Now you can use custom/ folder under resources/ to serve static files.

This is also possible using Java config in Spring Boot 2:

@Configuration public class StaticConfig implements WebMvcConfigurer < @Override public void addResourceHandlers(ResourceHandlerRegistry registry) < registry.addResourceHandler("/static/**").addResourceLocations("classpath:/custom/"); >> 

This confugration maps contents of custom directory to the http://localhost:8080/static/** url.

answered Feb 19, 2018 at 9:09 Hamid Mohayeji Hamid Mohayeji 4,205 3 3 gold badges 48 48 silver badges 58 58 bronze badges

What if I want the file in the static folder to be returned by a Spring controller? Which path should I use in new File() ?

Commented Apr 27, 2023 at 19:46

I am using :: Spring Boot :: (v2.0.4. RELEASE ) with Spring Framework 5

Spring Boot 2.0 requires Java 8 as a minimum version. Many existing APIs have been updated to take advantage of Java 8 features such as: default methods on interfaces, functional callbacks, and new APIs such as javax.time.

Static Content

By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/ resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so that you can modify that behavior by adding your own WebMvcConfigurer and overriding the addResourceHandlers method.

By default, resources are mapped on /** and located on /static directory. But you can customize the static loactions programmatically inside our web context configuration class.

@Configuration @EnableWebMvc public class Static_ResourceHandler implements WebMvcConfigurer < @Override public void addResourceHandlers(ResourceHandlerRegistry registry) < // When overriding default behavior, you need to add default(/) as well as added static paths(/webapp). // src/main/resources/static/. registry //.addResourceHandler("/**") // « /css/myStatic.css .addResourceHandler("/static/**") // « /static/css/myStatic.css .addResourceLocations("classpath:/static/") // Default Static Loaction .setCachePeriod( 3600 ) .resourceChain(true) // 4.1 .addResolver(new GzipResourceResolver()) // 4.1 .addResolver(new PathResourceResolver()); //4.1 // src/main/resources/templates/static/. registry .addResourceHandler("/templates/**") // « /templates/style.css .addResourceLocations("classpath:/templates/static/"); // Do not use the src/main/webapp/. directory if your application is packaged as a jar. registry .addResourceHandler("/webapp/**") // « /webapp/css/style.css .addResourceLocations("/"); // File located on disk registry .addResourceHandler("/system/files/**") .addResourceLocations("file:///D:/"); >> 
http://localhost:8080/handlerPath/resource-path+name /static /css/myStatic.css /webapp /css/style.css /templates /style.css 

In Spring every request will go through the DispatcherServlet. To avoid Static file request through DispatcherServlet(Front contoller) we configure MVC Static content.

As @STEEL said static resources should not go through Controller. Thymleaf is a ViewResolver which takes the view name form controller and adds prefix and suffix to View Layer.