Reputation: 159
I'm using JAX-RS and I want to display the HTTP status and an error message.
Example: HTTP 204 No Content
Here is my code:
public Response getMessageById(@Context HttpServletRequest request,
@PathParam("idMessage") BigInteger idMessage){
if (idMessage== null){
return Response.status(HttpURLConnection.HTTP_NO_CONTENT)
.entity("No Content").build();
}
}
It displays No Content
without the HTTP status.
Upvotes: 0
Views: 673
Reputation: 728
HTTP defines a response header and a response body. The latter one is set by calling entity()
, the former by status()
. So what you actually send is this:
204 No Content
No Content
You just don't see the header if the tool you use does not display it by default. Use for example curl -v http://your-rest-service/api/resource
so see it.
Furthermore:
@Path
.HttpURLConnection
is possible, it would be more consistent to use javax.ws.rs.core.Response.Status
HttpServletRequest
is for rare edge cases only. Don't use it if you don't need it.Upvotes: 1