Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - index.html not found by controller

Tags:

I use SpringBoot 1.5.9 and created src/main/resources/static/index.html:

<!DOCTYPE html><html><head><meta charset="utf-8"/>
<title>Home</title></head>
<body><h2>Hello World!!</h2></body>
</html>

My simple controller is handling "/" and forwarding to index.html:

@Controller
    public class MyController {
        @RequestMapping("/")
        public String home(Model model) {
            System.out.println("In MyController!!!");
            return "index";
    }}

However when after I run my main method from:

@SpringBootApplication
public class MySpringBoot2Application {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBoot2Application.class, args);
    }
}

and I point my browser to: http://localhost:8080/ I am getting: Whitelabel Error Page-There was an unexpected error (type=Not Found, status=404)

But, if i try to access the page directly - it works fine: http://localhost:8080/index.html

Based on the SpringBoot documentation the static content under src/main/resources/static/ should be rendered. If I create folder src/main/resources/templates/ and put my index.html there, and also include spring-boot-starter-thymeleaf dependency in my pom.xml then everything works good. However I am puzzled why this basic rendering of src/main/resources/static/index.html is not working. Any help is appreciated.

like image 860
icad Avatar asked Dec 15 '17 19:12

icad


1 Answers

Just found the solution: the controller MyController should specify the file extension:

return "index.html";
like image 117
icad Avatar answered Sep 21 '22 12:09

icad