我正在编写一个基于 REST 的 Web 服务,我正在尝试找出处理错误情况的最佳方法.
I'm writing a REST based web service, and I'm trying to figure out the best way to handle error conditions.
目前该服务正在返回 HTTP 错误,例如错误请求,但我如何返回额外信息以让使用 Web 服务的开发人员了解他们做错了什么?
Currently the service is returning HTTP Errors, such as Bad Request, but how can I return extra information to give developers using the web service an idea what they're doing wrong?
例如:创建用户名为空的用户返回错误请求.如何添加错误是由空用户名参数引起的?
For example: creating a user with a null username returns an error of Bad Request. How can I add that the error was caused by a null username parameter?
推荐答案HTTP 规范中规定,大多数错误代码应返回一些基本文本,以阐明返回错误的原因.基本的 Java Servlet 规范为此定义了 HttpServletResponse.sendError(int Code, String message).
It's stated in the HTTP spec that most error codes should return some basic text that gives a clarification of why the error is being returned. The basic Java Servlet Spec defines the HttpServletResponse.sendError(int Code, String message) for this purpose.
更多推荐
在调用 REST 服务期间传达错误消息的正确方法?
发布评论