In Thymeleaf, to trigger the index.html page (present in templates folder of resources) providing a new @RequestMapping method in the application's Main class does not work (outputs the string instead of rendering the HTML page) but creating a separate @Controller class with same @RequestMapping method is triggering the desired UI.
Method 1
The @RequestMapping method in Main class
package com.personal.boot.spring.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
@RequestMapping(value = "/index")
public String index() {
return "index";
}
}
Now, a get request to /index outputs "index" as a string
Method 2
The @RequestMapping method in a separate @Controller class
package com.personal.boot.spring.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WebController {
@RequestMapping(value = "/index")
public String index() {
return "index";
}
}
Now, a get request to /index renders the index.html page in templates folder.
Why is this the case? Please help.