我什么时候应该 return 从 REST 应用程序到客户端的 HTTP 状态代码 500(内部服务器错误)?

When should I return HTTP Status Code 500 (Internal Server Error) from REST application to client?

我见过有人使用 HTTP 代码 500 作为所有类型错误情况的通用错误代码(服务器错误、http 错误、代码异常、在数据库中未找到预期记录、超时异常等)。但是,我也读到并听说此代码 500 仅应在 应用程序服务器 (在我的情况下为 JBoss)出现错误的情况下使用,而不是 [=13] =]application 本身,即不适用于代码异常或无效密码或未找到数据库记录的情况。那么,我们什么时候应该 return 来自 REST 服务方法的 HTTP 状态代码 500?

如 HTTP 规范中所述:

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition. A user agent SHOULD display any included representation to the user. These response codes are applicable to any request method.

原文: https://greenbytes.de/tech/webdav/rfc7231.html#rfc.section.6.6

请注意,从协议的角度来看,无论是应用服务器还是应用服务器运行应用服务器内部的东西都没有关系。