Skip to content

Cannot open index page in Thymeleaf, getting 404 in Intellij IDEA

I’ve created simple demo for Spring boot and Thymeleaf, but I cannot open index page.

Controller code:

package com.jrp.demo.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HomeController {
 
    @RequestMapping(value = "/")
    public String home() {
        return "index";
    }
}

Having index template in /src/main/resources/templates/index.html

Having application.properties:

server.port=9095

And going to the address http://localhost:9095/

And getting error:

2021-12-10 11:08:32.200 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2021-12-10 11:08:32.200 DEBUG 11124 --- [nio-9095-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.jrp.demo.web.HomeController#home()
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.servlet.view.InternalResourceView  : View name 'index', model {}
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.servlet.view.InternalResourceView  : Forwarding to [index]
2021-12-10 11:08:32.201 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index", parameters={}
2021-12-10 11:08:32.202 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-12-10 11:08:32.204 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-12-10 11:08:32.207 DEBUG 11124 --- [nio-9095-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-12-10 11:08:32.220 DEBUG 11124 --- [nio-9095-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-12-10 11:08:32.220 DEBUG 11124 --- [nio-9095-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Spring boot class is:

package com.jrp.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

and pom.xml contains spring-boot-starter-thymeleaf and spring-boot-starter-web.

Answer

As described in following answer, Intellij IDEA had outdated dependencies, so helped buttons Reload all maven projects and Generate sources and update folders for all projects. enter image description here