返回哪个 HTTP 响应状态

发布于 2024-12-12 07:35:22 字数 180 浏览 2 评论 0原文

我有一个 REST 操作。用户想要删除一个对象。

  • 如果发生数据库错误
  • 当使用 response.getWriter() 写入响应时,如果发生 IOException。

我应该返回哪个状态码(500、503等)给客户端。(我的意思是哪个更方便?)

I have a REST operation. User wants to delete an object.

  • If a database error occurs
  • When writing to response with response.getWriter() if IOException occurs.

Which status code(500, 503 etc.) should I return to the client side.(I mean which one is more convenient?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挖鼻大婶 2024-12-19 07:35:22

500 在这里听起来最合适,除非您知道这是由于服务器过载造成的。

如果 response.getWriter() (或稍后对 writer 的调用)抛出 IOException,那么我怀疑您已经超出了能够有效影响响应的阶段反正客户已经收到了...

500 sounds most appropriate here, unless you know it's due to server overload.

If response.getWriter() (or a later call to the writer) throws an IOException, then I suspect you're beyond the stage of being able to usefully affect the response received by the client anyway...

时间你老了 2024-12-19 07:35:22

500个代码并不多,大部分都有含义 除了“数据库失败”/“发生ioException”之外。请注意,您可能应该区分哪种数据库错误会导致 404 状态,例如删除不存在的对象(请记住检查 DELETE 语句的“受影响的行”)。

对于您的两个示例,您应该返回状态 500。尽管如果在 response.getWriter() 上发生 IO 异常,则返回错误可能为时已晚,或者无论如何您都无法到达客户端。

并且,查看 twitter api 使用的响应代码以及页面。

There isn't that many 500 codes, and most of them have a meaning other than "database failed"/"ioexception occured". Note that you should probably distinguish between what kind of database error, e.g. deleting a non existant object (remember to check the "affected rows" of your DELETE statement), would result in a 404 status.

For your two examples, you should return status 500. Though if an IO exception occur on the response.getWriter(), it's likely too late to return an error, or you can't reach the client anyhow.

And, take a look at the response codes used by the twitter api as well as this page.

黑寡妇 2024-12-19 07:35:22

如果是临时错误,503 是合适的,因为它通常意味着“稍后重试”。但我认为大多数数据库错误不属于这一类。在大多数情况下,通用 500 是最佳响应。 500 列表中没有太多选项:

http://en.wikipedia.org/wiki/ List_of_HTTP_status_codes#5xx_Server_Error

如果由于删除是无效操作或类似的原因而导致错误,则 403 禁止是什么我会用:)

If it's a temporary error, 503 is appropriate as it generally means "retry later". I don't think most database errors fall into that category though. In most cases a generic 500 is the best response. There aren't many options in the 500 list:

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error

If it's an error due to the delete being an invalid operation or something like that, 403 forbidden is what I'd use :)

负佳期 2024-12-19 07:35:22

您没有向我们提供足够的信息来回答有关数据库错误的问题。如果数据库错误是由于违反引用完整性约束而导致的,那么您应该返回 400 错误,因为这是客户端的错误。

您担心什么样的数据库错误?

You have not provided us with sufficient information to be able to answer the question regarding database errors. If the database error is caused due to violating a referential integrity constraint then you should be returning a 400 error because it is an error on the part of the client.

What kind of database errors are you concerned about?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文