Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to throw HTTP 204 status code using jersey framework in RESTful web service?

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?

like image 819
Ashmi Suranse Avatar asked Dec 31 '12 14:12

Ashmi Suranse


People also ask

Does the HTTP status code 204 represent in a REST service?

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.

What does status code 204 no content usually stands for?

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.

What is the difference between 200 and 204 status code?

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.


2 Answers

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().

like image 200
Ryan Stewart Avatar answered Oct 18 '22 13:10

Ryan Stewart


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();
}
like image 37
secondflying Avatar answered Oct 18 '22 11:10

secondflying