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

@GetMapping不生效求助:Spring Boot接口返回白标错误页

Spring Boot接口返回Whitelabel Error Page,无法返回Student列表的排查方案

问题1:Student类缺少无参构造函数

Spring MVC用Jackson序列化对象时,默认要求目标类提供无参构造函数,否则无法实例化对象完成序列化。你的Student类只定义了两个带参构造,没有无参构造,这会导致接口返回时序列化失败,最终触发Whitelabel错误页。

解决方法:在Student类中添加无参构造:

public Student() {
    // 无参构造,Jackson序列化必备
}

问题2:确认接口访问路径

你的@GetMapping未指定路径,默认映射到根路径/,请确认访问地址是http://localhost:8080/(端口未修改的情况下),访问其他路径会触发404错误。

如果需要自定义路径,可修改注解:

@GetMapping("/students")
public List<Student> hello() {
    // ... 原代码逻辑
}

此时访问http://localhost:8080/students即可。

优化建议:导入语句规范

Demo2Application中的import com.example.demo.Student.*;虽能正常工作,但更清晰的写法是直接导入具体类:

import com.example.demo.Student.Student;

避免后续包内新增类时出现潜在的命名冲突。

验证步骤

  1. 添加无参构造后重启Spring Boot应用
  2. 访问对应路径(根路径或自定义的/students)
  3. 确认页面返回JSON格式的Student列表

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 04:30:43