How to resolve org.thymeleaf.exceptions.TemplateInputException: Error resolving template, template might not exist or might not be accessible by any of the configured Template Resolvers when using spring framework with thymeleaf
Problem
When running a Spring Boot or Spring MVC application with Thymeleaf, you might encounter the following error when visiting a URL:
curl http://127.0.0.1:8080/hello2The error message is:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Feb 24 11:10:36 CST 2021There was an unexpected error (type=Internal Server Error, status=500).Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolversorg.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template ResolversThe core exception is:
There was an unexpected error (type=Internal Server Error, status=500).Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolversorg.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template ResolversSpring framework is complaining that it cannot find the view resolver for the specified URL.
Environment
- Spring Boot 1.x and 2.x
The Codes and Configurations
The project layout is as follows:
.└── app/ ├── build.gradle └── src/ └── main/ ├── java/ │ └── com.bswen.app9/ │ ├── MvcConfig │ ├── HelloController │ └── Main └── resources/ ├── templates/ │ └── hello.html └── application.propertiesThe build.gradle file includes the necessary dependencies:
dependencies { ... implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' ....}The HelloController defines a method to handle HTTP GET requests for the /hello2 URL:
@Controllerpublic class HelloController { @GetMapping("/hello2") public String hello2(Authentication authentication) { return String.format("hello2 %s", authentication.getName()); }}There is also a configuration class to configure Spring MVC view controllers:
package com.bswen.app9.config;
import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configurationpublic class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); }}However, running the application results in the following exception:
There was an unexpected error (type=Internal Server Error, status=500).Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolversorg.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template ResolversSolution
Step 1: Remove the WebMvcConfigurer Subclass
Since Spring MVC has a default view resolver for Thymeleaf, you do not need to configure the views explicitly. Remove the MvcConfig class. The directory structure now looks like this:
.└── app/ ├── build.gradle └── src/ └── main/ ├── java/ │ └── com.bswen.app9/ │ ├── HelloController │ └── Main └── resources/ ├── templates/ │ └── hello.html └── application.propertiesStep 2: Modify the Controller
Update the HelloController to include the @ResponseBody annotation:
@Controllerpublic class HelloController { @GetMapping("/hello2") public @ResponseBody String hello2(Authentication authentication) { return String.format("hello2 %s", authentication.getName()); }}The @ResponseBody annotation indicates that the method returns a plain string and should not be resolved as a view template.
After making these changes, rerun the application. The error should disappear:
➜ bswen-springboot23 git:(main) ✗ curl http://127.0.0.1:8080/hello2hello2 bswen%It works!
About the View Resolver in Spring MVC
Spring relies on the view resolver to resolve the view name from the controller. Thymeleaf is a Java library that acts as a template engine for XML/XHTML/HTML5. It applies transformations to template files to display data or text produced by your applications.
If you want to use Thymeleaf, simply add the dependency to your project.
All the example code and configuration files can be found in this GitHub project.
Summary
In this post, we explored how to resolve the TemplateInputException in Spring Boot applications using Thymeleaf. The key steps include removing unnecessary view configurations and using the @ResponseBody annotation to indicate that a method returns a plain string. By following these steps, you can avoid common pitfalls related to view resolution in Spring MVC.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!