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

Spring Boot项目出现Whitelabel Error Page 404问题求助

问题分析与解决方案

这个问题的核心是你的Spring WebFlux应用没有识别到/hello的控制器方法,导致请求进来后只能匹配到静态资源处理器,最终返回404错误。

为什么会出现这个问题?

你的DemoApplication类只标注了@SpringBootApplication,但缺少控制器相关的注解(比如@RestController或@Controller)。Spring WebFlux只会把带有这些注解的类中的@GetMapping等请求映射方法识别为控制器接口,否则这些方法不会被注册到路由体系中。

这就解释了日志里的现象:请求/hello时,Spring找不到对应的控制器逻辑,只能尝试去静态资源目录里查找这个路径,自然找不到,于是返回404,也就是日志中显示的Mapped to ResourceWebHandler和Resource not found。

修复方案

你有两种简单的修复方式,推荐第二种更符合代码规范的写法:

方式一:给启动类添加@RestController注解

直接在DemoApplication类上加上@RestController,让Spring识别这个类中的方法为控制器接口:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController // 添加这个注解
public class DemoApplication {
 public static void main(String[] args) {
 SpringApplication.run(DemoApplication.class, args);
 }

 @GetMapping("/hello")
 public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
 return String.format("Hello %s!", name);
 }
}

方式二:拆分控制器到单独类(推荐的最佳实践)

把控制器逻辑抽成独立的类,让启动类只负责应用启动,代码结构更清晰:

// DemoApplication.java(仅保留启动逻辑)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
 public static void main(String[] args) {
 SpringApplication.run(DemoApplication.class, args);
 }
}
// 创建HelloController.java
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
 @GetMapping("/hello")
 public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
 return String.format("Hello %s!", name);
 }
}

验证修复

修改后重启应用,访问http://localhost:8080/hello,或者带参数的http://localhost:8080/hello?name=Mariangela,就能得到预期的响应:Hello World!(或Hello Mariangela!)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 06:42:55