TemplateInputException when Thymeleaf SpringTemplateResolver cannot resolve login template
0 reputation · 15 May 2020, 23:13 UTC
0 reputation · 15 May 2020, 23:13 UTC
The goal is to understand whether Thymeleaf should immediately throw a TemplateInputException when a template cannot be resolved, or whether it should permit a fallback view when throwExceptionIfNotFound is set to false, especially given that the default value changed between Thymeleaf 3.0.x and 3.1.x.
Constraints include the version‑dependent default, the effect of template caching on when the exception is surfaced, and the fact that a custom DatabaseTemplateResolver that relies on missing credentials will still propagate the same exception regardless of the flag.
Uncertainty remains about the recommended configuration for applications that want missing templates to be handled by a custom error view without exposing internal resolution failures, and how caching interacts with this setting in production versus test environments.
What value should throwExceptionIfNotFound be set to in a Spring Boot application using Thymeleaf 3.1.x to allow a custom error view to render instead of the exception?
How does enabling template caching (spring.thymeleaf.cache = true) affect the timing of the TemplateInputException when throwExceptionIfNotFound is false?
Should a DatabaseTemplateResolver that receives null credentials be expected to throw TemplateInputException even when throwExceptionIfNotFound is disabled?
34775 reputation · 16 May 2020, 05:57 UTC
In a Spring Boot application on Thymeleaf 3.1.x, set throwExceptionIfNotFound to false if you want a missing template to fall through to your custom error view instead of surfacing a TemplateInputException. In 3.1.x this is already the default, so the main work is verifying your view resolver chain actually handles the null result — and understanding that the flag only governs SpringTemplateResolver, not every resolver in your chain.
When throwExceptionIfNotFound is true, SpringTemplateResolver throws TemplateInputException as soon as it cannot resolve a template. When it is false, resolution returns null instead, which lets ThymeleafViewResolver (or your own error handling) treat the miss as a renderable fallback rather than a hard failure. The default changed between 3.0.x and 3.1.x, so behavior can flip silently after an upgrade — worth confirming explicitly rather than relying on the default.
# application.properties (Spring Boot 3.x / Thymeleaf 3.1.x)
spring.thymeleaf.throw-exception-if-not-found=false
If the property appears to be ignored in your setup, define the resolver bean explicitly — that is the reliable path:
@Bean
public SpringTemplateResolver templateResolver() {
SpringTemplateResolver r = new SpringTemplateResolver();
r.setPrefix("classpath:/templates/");
r.setSuffix(".html");
r.setThrowExceptionIfNotFound(false);
return r;
}
With spring.thymeleaf.cache=true, the outcome of the first resolution attempt for a given template name is cached. If the first request for a missing template resolves to null (because the flag is false), that null result is what gets cached: subsequent requests short-circuit to the fallback immediately, with no re-resolution and no exception. The practical consequence:
So the exception "timing" question has a subtle answer: with the flag off there is no exception from SpringTemplateResolver at all; caching only determines how often resolution is attempted.
Yes — a custom DatabaseTemplateResolver that throws when credentials are null will propagate TemplateInputException regardless of the flag. throwExceptionIfNotFound is a convention on SpringTemplateResolver, not a contract of the ITemplateResolver interface. If your custom resolver should honor the same fallback behavior, it must explicitly return null on "not found" conditions instead of throwing. A resolver that throws on infrastructure problems (missing credentials) is arguably correct — that is a configuration error, not a missing template — but be aware it bypasses your error view entirely.
TemplateInputException reaches the error handler.One caveat: exact property support and default values should be checked against your specific Spring Boot / Thymeleaf versions, since this behavior shifted across releases. Verify against your dependency tree before relying on defaults.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.