Jersey's “NoContent” response returns 200 instead of 204

I am using Jersey (1.18) to create a REST API for my WebApplication. In 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;

According to the Jersey documentation, NO_CONTENT should return code 204 , instead the http response has a header with code 200 .

NO_CONTENT
public static final ClientResponse.Status NO_CONTENT
204 No content, see HTTP / 1.1 Documentation.

I tried changing the above code to

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

But the problem still exists. As a note, using NOT_FOUND instead of NO_CONTENT , return the 404 header as expected.

Any sentence “How can I return the code 204?” Is a mistake, or am I doing something wrong.

Note. Not a duplicate Returning 200 response code instead of 204

+5
source share
2 answers

See this SO answer that says:

... 204 means "No content", which means that the answer does not contain but you put it in it. Jersey probably switches it to 200 for you, which is basically identical to 204, except that it contains the response object.

Finally, you can get 204 responses very simply with a few built-in behaviors: void methods and null return values ​​equal to 204 response. Otherwise, just return Response.status(204).build() .

In other words, if you want "NO_CONTENT" then do not include the content in your answer.

+6
source

After a bit more digging, I found a problem. W3c Documentation gives a hint.

I quote

10.2.5 204 No maintenance

The server has completed the request, but it does not need to return the body of the entity and may want to return updated meta-information. The answer MAY include new or updated meta-information in the form of entity headers, which, if they MUST be associated with the requested option.

If the client is a user agent, he SHOULD NOT change the look of his document from what caused the request to be sent. This answer is primarily intended to allow entry for actions without causing a change in the presentation of the active user document, although any new or updated meta information MUST apply to the document currently in the active view of the user agent.

Answer 204 MUST NOT include the message body and therefore always ends with the first empty line after the header fields.

In my code, I have entity(err_message) , which is causing the problem. Having deleted it, returns 204 . I think somehow Jersey or “someone” drops the answer to 200 because it has content.

Update (02/05/2015)

This blog post (posted earlier today as an answer and then deleted) provides additional information about the situation. Based on the content of the blog post, whenever the content of the HTTP response is present, the following method is called. This method returns a status code of 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 discovers that since there is some content in the response, the status code was set incorrectly to 204, and it changes it to 200.

+3
source

All Articles