在调用 REST 服务期间传达错误消息的正确方法?

编程入门 行业动态 更新时间:2024-10-20 13:30:43
本文介绍了在调用 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 服务期间传达错误消息的正确方法?

本文发布于:2023-11-26 18:19:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:传达   正确   错误   消息   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!