In REST / Java, what should I return if my object is zero?

I have a simple POJO that I marked 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 generate an NPE, which is then intercepted and logged. It is not beautiful, but it is not the main thing.

Ultimately, if d has a value or not, I return it.

If the value is zero, my client receives an HTTP status code of 204. Here is what wget displays: HTTP request sent, awaiting response... 204 No Content

Oddly enough, my browsers did not move an inch. They still display the previous page (I suppose it makes sense to stay in place when the content is not received). I would expect a blank page.

Three questions:

  • HTTP 204 correct answer to be returned?
  • How can I control this with annotations? Through a different configuration?
  • What is the standard REST recommendation for null objects?

Thanks

EDIT

There is a big question on the same topic: Is it correct to return 404 if the REST resource is not found?

+6
source share
1 answer

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.

+12
source

Source: https://habr.com/ru/post/978022/


All Articles