简体   繁体   English

Jersey'NoContent'响应返回200而不是204

[英]Jersey 'NoContent' response returns 200 instead of 204

I am using Jersey (1.18) to build a REST API for my WebApplication. 我正在使用Jersey(1.18)为我的WebApplication构建REST API。 In a part of my code I have the following snippet. 在我的部分代码中,我有以下代码段。

return Response.status(Status.NO_CONTENT).entity(err_message).build();

where Status is an instance of com.sun.jersey.api.client.ClientResponse.Status; 其中Statuscom.sun.jersey.api.client.ClientResponse.Status;的实例com.sun.jersey.api.client.ClientResponse.Status;

According to Jersey Documentation NO_CONTENT should return a 204 code, instead of this, the http response has a header with 200 code. 根据Jersey文档, NO_CONTENT应该返回一个204代码,而不是这个,http响应有一个包含200个代码的头。

NO_CONTENT 无内容
public static final ClientResponse.Status NO_CONTENT public static final ClientResponse.Status NO_CONTENT
204 No Content, see HTTP/1.1 documentation. 204 No Content,请参阅HTTP / 1.1文档。

I tried to change the aforementioned code to 我试图将上述代码更改为

return Response.noContent().entity(err_message).build();

But the issue still exists. 但问题仍然存在。 As a side note, using NOT_FOUND instead of NO_CONTENT , return a 404 header as expected. 作为旁注,使用NOT_FOUND而不是NO_CONTENT ,按预期返回404标头。

Any suggestion on 'How can I return 204 code?', is this a bug or I am doing something wrong. 关于'如何返回204代码?'的任何建议,这是一个错误或我做错了什么。

Note: Not a duplicate of Returning 200 response code instead of 204 注意:不是返回200响应代码而不是204的重复

See this SO answer which says, 看到这个SO答案说,

...204 means "No Content", meaning that the response contains no entity, but you put one in it. ... 204表示“无内容”,意味着响应不包含任何实体,但您在其中放入了一个实体。 It's likely that Jersey is switching it to a 200 for you, which is basically identical to a 204 except that it contains a response entity. 泽西岛很可能会把它换成200,除了包含响应实体之外,它基本上与204相同。

Finally, you can get 204 responses very simply by a couple of built-in behaviors: void methods and null return values both map to a 204 response. 最后,您可以通过几个内置行为非常简单地获得204个响应:void方法和null返回值都映射到204响应。 Otherwise, simply return Response.status(204).build() . 否则,只需返回Response.status(204).build()

In other words, if you want "NO_CONTENT" then don't include content in your response. 换句话说,如果您想要“NO_CONTENT”,则不要在回复中包含内容。

After a little more digging I have found the problem. 经过一点挖掘,我发现了问题。 The W3c Documentation gives a hint. W3c文档提供了一个提示。

I am quoting 我在引用

10.2.5 204 No Content 10.2.5 204没有内容

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. 服务器已完成请求但不需要返回实体主体,并且可能希望返回更新的元信息。 The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant. 响应可以包括实体标题形式的新的或更新的元信息,如果存在,应该与所请求的变体相关联。

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. 如果客户端是用户代理,它不应该从导致请求发送的文档视图中更改它的文档视图。 This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view. 此响应主要用于允许在不导致更改用户代理的活动文档视图的情况下进行操作的输入,尽管任何新的或更新的元信息应该应用于当前在用户代理的活动视图中的文档。

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields. 204响应绝不能包含消息体,因此总是在头字段之后的第一个空行终止。

In my code I have entity(err_message) which causes the problem. 在我的代码中,我有entity(err_message)导致问题。 By removing it the 204 is returned correctly. 通过删除它,正确返回204 I think somehow the Jersey or 'someone' casts the response to 200 since it has content. 我认为不管怎么说泽西岛或者'某人'因为它有内容而对200回应。

Update (02/05/2015) 更新(02/05/2015)

This blog post link (posted earlier today as an answer and then deleted), gives some additional insights about situation. 博客帖子链接(今天早些时候作为答案发布,然后删除),提供了一些关于情况的额外见解。 Based on the content of the blog post, whenever there is any content in the HTTP response the following method is invoked. 根据博客文章的内容,只要HTTP响应中有任何内容,就会调用以下方法。 This method sets the status code back to 200. 此方法将状态代码设置回200。

private void commitWrite() throws IOException {
   if (!isCommitted) {
       if (getStatus() == 204)
           setStatus(200);
       isCommitted = true;
       o = responseWriter.writeStatusAndHeaders(size, ContainerResponse.this);
   }
}

We can say that Jersey detects that since there is some content in the response, the status code was wrongly set to 204 and it changes it to the appropriate 200. 我们可以说Jersey检测到由于响应中有一些内容,状态代码被错误地设置为204并将其更改为适当的200。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM