如何发送服务器错误响应代码500的JSON数据

发布于 2024-09-05 10:48:00 字数 450 浏览 2 评论 0原文

我想知道是否有办法发送 JSON 数据以及 HTTP 响应代码 500。基本上,我希望我的其余客户端知道后端存在一些错误,并随之发送一个像这样的 JSON 错误数据结构。

{"error" : [
          {"code": "1001", "desc": "Some error description"},
          {"code": "1002", "desc": "Some other error description"}
           ]
}

这是使用以下 java 堆栈 = Java 6/JAX-RS/Jersey/Tomcat

如果没有,那么有没有办法将自定义响应代码与 JSON 数据一起发送。基本上从 JAX-RS API 来看,你只能发送 JSON 数据以及 200 OK??

有什么想法吗?我猜 RESTEasy 也会一样,对吗?

I wanted to know if theres a way to send a JSON data along with HTTP response code 500. Basically I want my rest client to know that there is some error on the backend and along with it send a JSON error data structure like this.

{"error" : [
          {"code": "1001", "desc": "Some error description"},
          {"code": "1002", "desc": "Some other error description"}
           ]
}

This is using the following java stack = Java 6/JAX-RS/Jersey/Tomcat

If not, then is there a way to send a custom response code along with JSON data. Basically looking from JAX-RS API it looks that you can only send JSON data along with 200 OK??

Any thoughts?? I am guessing RESTEasy would be the same, right??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-09-12 10:48:00

您只需将 HTTP 标头响应设置为您想要的任何内容,然后再开始使用 setStatus() Response.status(500)

You simply need to set the HTTP header response to whatever you want before starting to output the actual response using something like setStatus(). Response.status(500)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文