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

Spring Boot中JSP页面无法渲染,出现404白标错误页问题咨询

嘿,我刚上手Spring Boot的时候也踩过这个一模一样的坑!咱们一步步来排查解决:

排查Spring Boot白标404错误的常见方案

1. 补全Web开发核心依赖

你提到pom里有tomcat-embed相关,但单独引入这个包是不够的——Spring Boot开发Web应用最省心的方式是直接引入Web Starter依赖,它已经打包了tomcat、Spring MVC、Jackson等所有Web开发必需的组件,能帮你自动完成基础配置。

把你的依赖替换成这个:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

单独引入tomcat嵌入包会缺少Spring MVC的核心映射配置,这是导致请求找不到的常见原因之一。

2. 确认控制器的注解和路径是否正确

确保你的控制器类和方法标注了正确的注解:

  • 如果是返回JSON/字符串接口,用@RestController;如果是返回HTML页面,用@Controller
  • 方法上必须有@GetMapping/@PostMapping(或通用的@RequestMapping)指定访问路径

给你一个最简单的可运行示例:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

如果要返回HTML页面,记得额外引入模板引擎依赖(比如spring-boot-starter-thymeleaf),并把页面放在src/main/resources/templates目录下。

3. 检查启动类的位置是否合规

Spring Boot的启动类(带@SpringBootApplication的类)必须放在根包路径下,这样它才能自动扫描到子包中的控制器。比如:

  • 启动类包:com.example.demo
  • 控制器类包:com.example.demo.controller

如果启动类和控制器不在同一个根包下,需要手动指定扫描路径:

@SpringBootApplication(scanBasePackages = "com.example")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

4. 清理缓存并重新构建项目

有时候IDE缓存或构建残留会导致奇怪的问题,试试这两步:

  • 执行mvn clean install(Maven项目)或者./gradlew clean build(Gradle项目)
  • 重启IDE和应用

5. 查看启动日志找线索

启动应用时,仔细看控制台日志:

  • 确认Tomcat正常启动(会显示Tomcat started on port(s): 8080 (http))
  • 确认控制器被Spring扫描到(日志里会有类似Mapped "{[/]}" onto public java.lang.String com.example.demo.controller.HelloController.hello()的记录)

如果日志里没看到控制器的映射记录,那大概率是扫描范围或注解的问题,回到前面的步骤重新检查。


内容的提问来源于stack exchange,提问作者Stark

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:43:10