在调用 REST 服务期间传达错误消息的正确方法? [英] Proper way to convey error messages during calls to a REST service?

查看:38
本文介绍了在调用 REST 服务期间传达错误消息的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于 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 服务期间传达错误消息的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆