Spring Boot @Getmapping returns 一个非常简单的项目中的 404 whitelabel 错误页面

Spring Boot @Getmapping returns a 404 whitelabel error page in a very simple project

我正在学习spring引导。我创建了这个超级简单的项目,但是当我尝试 return 带有 @GetMapping 注释的 HTML 页面时,我不断收到 404 Whitelabel 错误页面。

这是我唯一的控制器:

package com.example.springplay;

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

@Controller
public class MainController {
    @GetMapping(value = "/")
    public String hello(){
        return "hello";
    }
}

这是 spring 申请:

package com.example.springplay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SpringplayApplication {

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


}

Here is the directory

这是 hello.html 页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

hello.html页面在resources/templates文件夹中,不知道怎么回事。 我从另一个工作项目中复制了这个具有完全相同结构的部分,但我的只是给了我这个 Whitelabel 错误页面。

将 hello.html 移动到资源中的静态文件夹并像这样更改控制器:

    @GetMapping(value = "/")
    public String hello(){
        return "hello.html";
    }

或者像这样:

    @GetMapping(value = "/")
    public ModelAndView  hello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello.html");
        return modelAndView;
    }