How can I serve static html from spring boot?

JavaSpring Boot

Java Problem Overview

I ran the spring-boot-sample-web-static project from [here][1], made this alteration to the pom


And added this class to serve a duplicate page index2.html from the same static folder location:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class Rester {

    @RequestMapping(value = "/rand", produces = MediaType.APPLICATION_JSON_VALUE)
    private RandomObj jsonEndpoint() {
        return new RandomObj();

    @RequestMapping(value = "/tw")
    public String somePg() {
        return "index2";

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]     : 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 to false

Am I doing something wrong? [1]:

Java Solutions

Solution 1 - Java

Static files should be served from resources, not from controller. > Spring Boot will automatically add static web resources located within > any of the following directories: > > /META-INF/resources/
> /resources/
> /static/
> /public/


Solution 2 - Java

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

For example:


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

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

public class StaticConfig implements WebMvcConfigurer {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

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

Solution 3 - Java

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 {

	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/...
			//.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/...
			.addResourceHandler("/templates/**") // « /templates/style.css
		// Do not use the src/main/webapp/... directory if your application is packaged as a jar.
			.addResourceHandler("/webapp/**") // « /webapp/css/style.css
		// File located on disk

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

Solution 4 - Java

As it is written before, some folders (/META-INF/resources/, /resources/, /static/, /public/) serve static content by default, conroller misconfiguration can break this behaviour.

It is a common pitfall that people define the base url of a controller in the @RestController annotation, instead of the @RequestMapping annotation on the top of the controllers.

This is wrong:

public class MyController {
    public String myPostMethod( ...) {

The above example will prevent you from opening the index.html. The Spring expects a POST method at the root, because the myPostMethod is mapped to the "/" path.

You have to use this instead:

public class MyController {
    public String myPostMethod( ...) {

Solution 5 - Java

I had to add thymeleaf dependency to pom.xml. Without this dependency Spring boot didn't find static resources.


Solution 6 - Java

You can quickly serve static content in JAVA Spring-boot App via thymeleaf (ref: source)

I assume you have already added Spring Boot plugin apply plugin: 'org.springframework.boot' and the necessary buildscript

Then go ahead and ADD thymeleaf to your build.gradle ==>

dependencies {

Lets assume you have added home.html at src/main/resources To serve this file, you will need to create a controller.


  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;

  public class HomePageController {

      public String homePage() {
        return "home";


Thats it ! Now restart your gradle server. ./gradlew bootRun


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionosmingoView Question on Stackoverflow
Solution 1 - JavacsengaView Answer on Stackoverflow
Solution 2 - JavaHamid MohayejiView Answer on Stackoverflow
Solution 3 - JavaYashView Answer on Stackoverflow
Solution 4 - JavaSaWoView Answer on Stackoverflow
Solution 5 - JavaKike LebowskiView Answer on Stackoverflow
Solution 6 - JavaSTEELView Answer on Stackoverflow