I have a simple POJO that I annotated with REST annotations as follows:
@GET
@Path("/domains/{domainid}")
@Override
public Domain getDomain(@PathParam("domainid") UUID domainID) throws Exception {
logger.info("Retrieving domain "+ domainID);
Domain d = null;
try {
d = MyClient.getDomains().get(domainID.toString());
logger.debug("Returning "+d.getName());
} catch (Exception e) {
logger.error("Could not retrieve domain", e);
}
return d;
}
Note that the log statement including d.getName()
can actually throw an NPE which is then caught and logged. That's not pretty but it's also not the point here.
Ultimately whether d
has a value or not, I return it.
In the case of a null
value, my client receives an HTTP 204 status code. This is what wget
displays: HTTP request sent, awaiting response... 204 No Content
Oddly enough, my browsers don't budge an inch. They still display the previous page (I suppose it makes sense to stay put when no content is received). I would have expected a blank page.
Three questions:
- is HTTP 204 the right response to be returned?
- how can I control that via annotations? Via other configuration?
- what is the standard REST best practice regarding null objects?
Thanks
EDIT
There is a great question on the very same topic here: Is it correct to return 404 when a REST resource is not found?