Error resolving template, template might not exist or might not be accessible by any of the configured Template Resolvers
Asked Answered
I

5

6

I have the following controller:

@Controller
public class ConfigController {

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

However, I receive the following error when going to the route /:

There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers

I have a config.html file inside of resources/templates:

enter image description here

My pom.xml file: https://pastebin.com/yqYYuXWh

What am I doing wrong?

I have already tried adding a .html to the return of the controller function, but it still doesn't work.


After removing the spring-web dependency (I already had spring-boot-starter-web) I now get the following error when starting spring:

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

Intermezzo answered 27/5, 2022 at 13:57 Comment(2)
You need to remove spring-web from your pom.xml. Also it seems like you added a lot of different sql connectors, normally you'd only need one.Frascati
@IdanElhalwani Sadly this application will be distributed and needs to connect to pretty much every DB there is.Intermezzo
F
5

The issue is in your pom.xml file.

By adding this block to your build;

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>application.properties</include>
        </includes>
    </resource>
</resources>

You have overriden spring-boot-starter-parent's own resources block and caused it to include application.properties and nothing else. Remove it from your pom.xml and delete your target directory.

Frascati answered 27/5, 2022 at 14:16 Comment(0)
S
3

This is a fix if someone has met all the requirements.Check the return value of the method. if you something like return '/admin/index' change it to return 'admin/index

Splay answered 2/2, 2023 at 14:36 Comment(0)
V
1
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.20</version>
    </dependency>

The second dependency is not required.


    <build>
<!--not required. Spring boot scans /resources directory by default 
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Please rebuild and restart the app again.

And you don't need to add .html extension in your controller. Spring boot looks for .html,.jsp extensions in src/main/resource/ ,src/main/resource/templates/ ,src/main/resource/static/ ,src/main/resource/public these directories by default

Viola answered 27/5, 2022 at 14:4 Comment(1)
I have removed the second dependency and now the following error appears when starting spring: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)Intermezzo
T
1

I'm using in developing Spring Boot appl'. To me below method works. That is change "**" set for 'Excluded' to (none).

enter image description here

Terrarium answered 1/10, 2023 at 1:49 Comment(2)
Could you specify which version of Eclipse you are using? I started getting this behaviour after switching from 2022-12 to 2023-12.Patricepatrich
That is, Eclipse(Spring Tool Suite 4 flavor, Version: 4.21.0.RELEASE)Terrarium
B
0

Annotate your handler method with @ResponseBody annotation like this public @ResponseBody String index() The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.

Benis answered 30/9, 2023 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.