Spring Boot: IndexController not needed

I added an IndexController.java file

public class IndexController {

    public String index(){
        return "index";

Because I thought I needed to map the web root path (“/”) to the index template. A common convention used in Spring MVC is to
return the string name of the template file, less the file extension. Thus to show the index.html file we
defined above, our controller method needs to return the string ‘index’,

On Line 6, I’ve used the @Controller annotation to make this class a Spring component and a Spring MVC
controller. On line 9, you can see that I annotated the index() method with a @RequestMapping annotation.
By doing this, I’ve configured the root path to this controller method.

But it was producing a 404 error saying /error mapping not found because it couldn’t find the index.html page

After researching on the web, I found that:

SpringBoot will serve static files from root URI automatically, without need to create any root level Controller. In fact your IndexController would prevent static front-end files to be served from root URI. There is no need to create Controller for static files at all.

Because your IndexController is mapped to “/”. When you want to serve static HTML files from your root resource, you shouldn’t have any Controller mapped to “/”

Once I deleted the IndexController file, the error went away.

Leave a Reply