简体   繁体   English

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

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

I am learning spring boot.我正在学习 spring 引导。 I created this super simple project, but I keep getting a 404 Whitelabel error page when I try to return an HTML page with the @GetMapping annotation.我创建了这个超级简单的项目,但是当我尝试返回带有 @GetMapping 注释的 HTML 页面时,我不断收到 404 Whitelabel 错误页面。

Here is my only controller:这是我唯一的 controller:

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";
    }
}

Here is the spring application:这是 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这是目录

Here is the hello.html page:这是 hello.html 页面:

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

The hello.html page is in the resources/templates folder, I don't know what's going on. hello.html页面在resources/templates文件夹下,不知道怎么回事。 I copied this part which has the exact same structure from another working project, yet mine just gives me this Whitelabel error page.我从另一个工作项目中复制了这个具有完全相同结构的部分,但我的只是给了我这个 Whitelabel 错误页面。

Move hello.html to static folder in resource and change controller like this:将 hello.html 移动到资源中的静态文件夹并像这样更改控制器:

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

or like this:或者像这样:

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

Remplace here替换这里

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

or like this或者像这样

@RequestMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
public String welcomepage() {
    return "hello";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM