升级Spring Boot后Thymeleaf找不到templates/error目录报错求助
问题分析与原因解释
错误信息
Caused by: java.io.FileNotFoundException: class path resource [templates/error] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:199) ~[spring-core-5.3.20.jar:5.3.20] at org.thymeleaf.spring5.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE] at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE] ... 90 common frames omitted
项目依赖
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.15.RELEASE</version> </dependency>
问题核心原因
Spring Boot版本升级后,ErrorMvcAutoConfiguration的错误视图解析逻辑发生了关键变化:
- 旧版本中,即便项目引入Thymeleaf依赖,系统触发错误页面渲染时,会优先使用默认的白标错误页面,不会触发Thymeleaf去
templates目录下查找错误模板资源。 - 新版本中,只要存在Thymeleaf依赖,Spring Boot会优先尝试通过Thymeleaf渲染错误页面,自动去
templates目录下查找error.html文件,或error文件夹下对应HTTP状态码的模板(比如404.html)。你的项目一直未提供该资源,因此触发了FileNotFoundException。
这个异常通常是Swagger UI请求过程中触发了某种错误(比如路径匹配异常、内容协商失败),导致Spring Boot尝试渲染错误页面,进而触发了Thymeleaf的资源查找逻辑。
验证与解决思路
- 临时验证:在
src/main/resources/templates下创建一个空的error.html文件,重启项目后查看异常是否消失。 - 配置调整:如果不需要自定义错误页面,可通过配置
server.error.whitelabel.enabled=true强制启用白标错误页面,跳过Thymeleaf的模板查找流程。 - Swagger配置排查:检查Spring Boot升级后Swagger的访问路径是否存在拦截冲突,导致请求触发错误跳转。
内容的提问来源于stack exchange,提问作者Deb Das
相关产品推荐
相关产品推荐

