Thymeleaf cannot detect templates inside spring-boot project


I have the following project structure in my Spring boot app, in which I want to use Thymeleaf

    -Gradle-Module1(Spring boot module)

but the spring-boot cannot find my template directory and is showing warning

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

PS: I am using @EnableAutoConfiguration

In my controller code I am doing something like:

public class BaseController {

    @RequestMapping(value = "/")
    public String index() {
        return "index.html";

and index.html file just prints hello world.

So typically it should look inside src/resources/templates/(of same Gradle module I suppose), but somehow it is not able to find it.

When I try to access localhost:8080 I am getting below error

Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers`

Is there anything I am missing?
Any pointers appreciated. Thanks.


You have to configure Thymeleaf as follows:

public class ThymeleafConfig {
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        return templateResolver;

    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        return springTemplateEngine;

    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        return viewResolver;

Spring doc recommends to add @EnableAutoConfiguration annotation to your primary @Configuration class.

Also it seems you have wrong project structure, the typical package hierarchy is:

  |- main
      |- java
      |- resources
          |- static
          |- templates
  |- test

In this case your templates will be in src/main/resources/templates, not in src/resources/templates/.

Answered By – DimaSan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published