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

Time:10-17

I am learning spring boot. 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.

Here is my only 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:

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:

<!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. I copied this part which has the exact same structure from another working project, yet mine just gives me this Whitelabel error page.

CodePudding user response:

Move hello.html to static folder in resource and change controller like this:

    @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;
    }
  • Related