I am using jersey framework to develop RESTful web service. I am throwing various HTTP status codes with response using following code:
public class RestNoContentException extends WebApplicationException
{
public RestNoContentException(String message)
{
super(Response.status(Status.NO_CONTENT)
.entity(message).type("text/plain")
.build());
}
}
While testing the REST web service using Firefox Mozilla rest client tool, it is displaying 200 OK
status instead of 204 NO CONTENT
. I am handling the other status codes the same way I am doing for status code 204
. Other status codes are appearing properly on rest client tool but when to show 204
status code, it is showing 200 OK
status code.
Can someone please help me out here? what am I missing?
The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.
5 204 No Content. 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.
The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.
First, 204 is in the "Successful" category of response codes, so returning it as the result of an exception is a very, very weird thing to do.
Second, 204 means "No Content", meaning that the response contains no entity, but you put one in it. 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.
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. Otherwise, simply return Response.status(204).build()
.
You shouldn't give entity if you want throw 204:
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response test() {
//return Response.status(Status.NO_CONTENT).entity("hello").build(); //this will throw 200
return Response.status(Status.NO_CONTENT).build();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With