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

Spring Boot 2.5.2搭配Thymeleaf访问现有接口返回404错误的原因是什么?

问题核心结论

你遇到的访问/greeting返回404问题为Spring Boot 2.5.2版本的兼容性问题导致,降级到2.4.10版本即可解决,和本地电脑的网络、代理、浏览器等配置无关。

可能成因

  • Spring MVC路径匹配策略变更:Spring Boot 2.5.x版本默认将Spring MVC的路径匹配策略从AntPathMatcher切换为PathPatternParser,部分场景下会导致控制器路由无法被正常识别,请求无法进入对应接口。
  • Thymeleaf版本适配Bug:Spring Boot 2.5.2内置的Thymeleaf 3.0.12.RELEASE版本在JDK 8环境下存在模板路径解析缺陷,即使模板文件存放在resources/templates正确路径下,也会被视图解析器判定为不存在,返回404状态码。
  • Maven资源过滤规则变更:Spring Boot 2.5.x对Maven默认资源打包规则做了调整,未显式配置resources节点的项目会出现templates目录下的HTML文件未被打包到classpath的问题,运行时无法读取模板。

不降级版本的解决方案

如果需要使用2.5.x版本,可通过以下任一配置修复问题:

  1. 切回旧版本路径匹配策略,在application.properties中添加配置:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  1. 手动指定兼容性更好的Thymeleaf版本,在pom.xml的properties节点添加配置:
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
  1. 显式配置Maven资源打包规则,在pom.xml的build节点添加配置:
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
    </resource>
</resources>

内容的提问来源于stack exchange,提问作者Илиян Д.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 04:36:05