0

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.

Veritas1832
  • 67
  • 3
  • 14

2 Answers2

1

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;
    }
Mehdi Varse
  • 271
  • 1
  • 15
  • 1
    Wow thanks a lot! It seems I didn't understand the difference between static and templates folders. I was following another tutorial where thymleaf was used and the HTML files were put in templates, that's why I got confused here. Thanks again! – Veritas1832 Oct 17 '21 at 07:48
0

Remplace here

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

or like this

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