You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:32:09