Search code examples
javajersey-2.0

Which is best between approach by returning HTTP OK response


I am writing an API with simple OK response, I have two options to archieve.

Please argument which is the best and why?

    1. Response.ok("OK").build();
    1. Response.status(200).entity("OK").build();

Solution

  • The status code is enough for OK response. So I would just use the simple:

    Response.ok().build();  
    

    Please note that Response.status(200).build() will do exactly the same but since you already have ok() method I think it makes the code simpler.

    You have the option to add entity to the OK response like you have written above but it makes sense only if the entity is meaningful. Everything being OK is clear enough from the OK status code so you don't need an entity here.