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

如何解决Spring Boot中的Whitelabel Error Page白标错误页面问题?

解决Spring Boot Whitelabel Error Page(404)问题

针对你碰到的404错误,结合你的代码和配置,按以下步骤排查:

1. 优先检查包扫描范围

你的HomeController在controllers包下,而Spring Boot启动类默认只会扫描自身所在包及其子包。如果你的启动类(比如TodoappApplication.java)在com.baothien包下,controllers和它同级,就会扫不到这个Controller。

解决办法:

  • 把controllers包移到com.baothien下面,变成com.baothien.controllers;
  • 或者在启动类上添加@ComponentScan指定扫描路径:
@SpringBootApplication
@ComponentScan(basePackages = {"controllers", "com.baothien"})
public class TodoappApplication {
    public static void main(String[] args) {
        SpringApplication.run(TodoappApplication.class, args);
    }
}

2. 确认访问地址是否正确

你的Controller映射的是/home,所以要访问http://localhost:8080/home(默认端口8080,没改配置的话),别直接访问根路径/,因为你没给根路径做映射。

3. 验证Thymeleaf模板配置和文件

虽然你已经引入了Thymeleaf依赖,还是要确认:

  • home.html确实在src/main/resources/templates/目录下,文件名大小写和Controller返回的"home"完全一致(比如Home.html和home.html会被当成不同文件);
  • 如果有application.properties,检查Thymeleaf默认配置没被修改错:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.enabled=true

4. 清理缓存重新构建

有时候是编译缓存或者依赖问题,执行:

  • 停掉应用;
  • 运行Maven命令:mvn clean install;
  • 重启应用,DevTools热重载如果开着的话,也可以试试手动触发重载。

5. 看日志找线索

启动应用后,盯着控制台日志,找有没有HomeController被加载的记录,比如:

INFO 12345 --- [  restartedMain] controllers.HomeController               : HomeController initialized

如果没看到这条日志,说明包扫描肯定有问题,回到第一步解决。


内容的提问来源于stack exchange,提问作者Hoàng Thiện

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 18:24:28