If a request tries to install / find / find a resource and it cannot be found, traditionally we should send 404 Not Found. For further discussion see here .
It seems to me that I usually like it when my resource methods return a Response , since itβs easier to set up the reaction the way I want (a little - not much - more details here ). But, seeing how your method redefines the interface contract (and returns the model object), JAX-RS gives us a good hierarchy of exceptions that will be mapped to a specific response / status. The list may be here .
So, in your specific case, if the resource is not found, you can throw a WebApplicationException(Response.Status.NOT_FOUND) or NotFoundException , and the exception will be mapped to 404 Not Found. Sort of
d = MyClient.getDomains().get(domainID.toString()); if (d == null) { throw new NotFoundException(); // <-- JAX-RS 2.0 // or throw new WebApplicationException(Response.Status.NOT_FOUND); // ^^ JAX-RS 1.x }
The method will exit when an exception is thrown and the client will receive a response with the status 404 Not Found.
Related Q & As
EDIT
In the first line, I said: βIf the request is trying to install / find / find the resource ..β, but in fact this applies to all cases that we use URI templates , whether for GET, POST, PUT, DELETE, whatever. Consider this example
@PUT @Path("/customers/{id}") public Response updateCustomer(@PathParam("id") long id, Customer customer) { ... }
Here is a way that allows the client to update the client through PUT. The client must know the full resource URI before trying to update it. If the parameter {id} (used for search) is not found, say in the database, then the resource does not exist, and 404 Not Found should also be returned to the client.