Java REST 中成功的 DELETE 语句的 HTTP 状态返回码是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29545861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the HTTP status return code for a successful DELETE statement in REST?
提问by AndreaNobili
I am studying how to Spring handle REST web services (but I don't know if it is a Spring related answer or more generically it is related only to REST concept).
我正在研究如何 Spring 处理 REST web 服务(但我不知道它是否与 Spring 相关的答案或更一般地说它仅与 REST 概念相关)。
So my doubt is: what exactly is the HTTP status return code for a successful DELETE statement?
所以我的疑问是:成功的 DELETE 语句的 HTTP 状态返回码究竟是什么?
Is it the 204or the 200?
是204还是200?
I know that the 200means that my request was correctly fulfilled but reading online it seems to me that it I expect it after a successful GET returning contentand not after a delete.
我知道200意味着我的请求得到了正确的满足,但在网上阅读在我看来,我希望它是在成功返回内容后而不是在删除后。
Somewhere I found that the 204status is obtained after successful PUT or DELETE. Is it true? I can't understand, it means that the response is empty, why an empty respons means that the PUT or the DELETE operation are gone succesfull?
在某处我发现204状态是在成功 PUT 或 DELETE 后获得的。这是真的吗?我无法理解,这意味着响应为空,为什么响应为空意味着 PUT 或 DELETE 操作成功?
采纳答案by Pedro Werneck
There are no strict rules on which HTTP status code is the correct one for each method. It depends on what exactly happened, what information you need to send to the client, etc. I can think of a few examples:
对于每种方法,对于哪个 HTTP 状态代码是正确的,没有严格的规则。这取决于究竟发生了什么,你需要向客户端发送什么信息等等。我可以举几个例子:
A successful
DELETE
, with no further information.204 No Content
A successful
DELETE
, but you have a warning about related orphan resources that should be deleted too.200 OK
.You accepted the
DELETE
request, but it might take a long time and you're going to do it asynchronously. The client should check it later.202 Accepted
.You accepted the
DELETE
request, but the resource can't be removed and the URI is instead reset to a default.205 Reset Content
.
一个成功的
DELETE
,没有进一步的信息。204 No Content
成功
DELETE
,但您有一个关于应该删除的相关孤儿资源的警告。200 OK
.您接受了
DELETE
请求,但可能需要很长时间,而且您将异步执行。客户应该稍后检查它。202 Accepted
.您接受了
DELETE
请求,但无法删除资源,而 URI 会重置为默认值。205 Reset Content
.
回答by Neil McGuigan
An empty response body doesn't mean that a delete is successful, a successful delete (usually) means that the response body is empty.
空响应正文并不意味着删除成功,成功删除(通常)意味着响应正文为空。
There's no official status code list for RESTful APIs, but most agree that a 204 is a good response code for a successful delete, as there's usually not a good reason to return a response body after deleting something.
没有 RESTful API 的官方状态代码列表,但大多数人都同意 204 是成功删除的良好响应代码,因为通常没有充分的理由在删除某些内容后返回响应正文。
In general, if an operation is successful and the response body is empty return 204. If an operation is successul and the response body is NOT empty, return 200
一般来说,如果操作成功并且响应体为空返回204。如果操作成功并且响应体不为空,则返回200
回答by Dave Newton
An empty response doesn'tmean the operation was successful, the HTTP error code is supposed to indicate success/failure, and the response body may or may not contain data.
空响应并不意味着操作成功,HTTP 错误代码应该指示成功/失败,并且响应正文可能包含也可能不包含数据。
The response body maycontain additional information regarding the request, e.g., a specific message to display to the UI, stats or timing info regarding the information, whatever. But it doesn't haveto, and the body's purpose is informational/diagnostic if it exists.
响应主体可以包含关于请求的附加信息,例如,要显示给 UI 的特定消息、关于该信息的统计信息或计时信息等。但它没有必要,如果存在,身体的目的是提供信息/诊断。
回答by honerlawd
2xx represents the request was successful. The xx just allows for you to be more specific about what happened (what the server did or is returning).
2xx 表示请求成功。xx 只是让您更具体地了解发生了什么(服务器做了什么或正在返回什么)。