Spring rest controller not returning html

Your example would be something like this:

Your Controller Method with your route "assessment"

@Controller
public class HomeController {

    @GetMapping("/assessment")
    public String index() {
        return "index";
    }

}

Your Thymeleaf template in "src/main/resources/templates/index.html"

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Hello World!</p>
</body>
</html>

I solved this by removing @EnableWebMvc annotation from configuration class.

Spring MVC Auto-configuration provides static index.html support.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

Get more detail from Spring MVC Auto-configuration.


RestController annotation returns the json from the method not HTML or JSP. It is the combination of @Controller and @ResponseBody in one. The main purpose of @RestController is to create RESTful web services. For returning html or jsp, simply annotated the controller class with @Controller.