如何发送服务器错误响应代码500的JSON数据
我想知道是否有办法发送 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将 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)