Spring Boot - 控制器未找到 index.html

ica*_*cad 5 spring-boot

我使用 SpringBoot 1.5.9 并创建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>
Run Code Online (Sandbox Code Playgroud)

我的简单控制器正在处理“/”并转发到 index.html:

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

但是,当我从以下位置运行我的主要方法后:

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

我将浏览器指向:http://localhost:8080/ 我得到:Whitelabel Error Page-There was an unexpected error(type=Not Found, status=404)

但是,如果我尝试直接访问该页面 - 它工作正常: http://localhost:8080/index.html

根据 SpringBoot 文档,src/main/resources/static/应该呈现下面的静态内容。如果我创建文件夹src/main/resources/templates/并将我的放在index.html那里,并且还在我的 pom.xml 中包含 spring-boot-starter-thymeleaf 依赖项,那么一切正常。但是我很困惑为什么这个 src/main/resources/static/index.html 的基本渲染不起作用。任何帮助表示赞赏。

ica*_*cad 5

刚刚找到解决方案:控制器 MyController 应该指定文件扩展名:

return "index.html";
Run Code Online (Sandbox Code Playgroud)