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

Spring Boot REST API报错时无法获取响应体问题求助

分享Spring Boot REST API错误响应的坑

我在这个问题上耗了不少时间,特意分享下我的发现——没想到这种几乎所有应用都必备的内容,居然找不到靠谱的参考。

我想要Spring Boot REST API在出错时,给客户端返回类似这样的额外数据:

>curl -i GET https://api.twitter.com/1.1/statuses/update.json?include_entities=true
HTTP/1.1 400 Bad Request
date: Fri, 14 Jul 2023 15:23:34 GMT
content-type: application/json; charset=utf-8
cache-control: no-cache, no-store, max-age=0
content-length: 62

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

用curl能正常拿到这个响应,但在浏览器里却收不到响应数据。我用Google开发者工具查了网络流量,确认我的REST API确实已经发送了数据。

之前找过两篇讲Spring REST错误处理的文章,没找到有用的信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 15:47:20