Reputation: 1827
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
:
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)
Upvotes: 6
Views: 20537
Reputation: 1403
I'm using in developing Spring Boot appl'. To me below method works. That is change "**" set for 'Excluded' to (none).
Upvotes: 1
Reputation: 41
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.
Upvotes: 0
Reputation: 822
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
Upvotes: 3
Reputation: 587
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.
Upvotes: 5
Reputation: 671
<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
Upvotes: 1