I have a call with a return object, but this call may return an empty result.
My URL in controller is:
/api/find-by-id/{id}
This controller make calls service as the following:
service.findById(Integer id);
My sevice searches through DAO:
repository.findById(Integer id);
When DAO return an empty value, should I send error HTTP 404
or HTTP 204
to front?
Considering:
2xx: Success - The action was successfully received, understood, and accepted
4xx: Client Error - The request contains bad syntax or cannot be fulfilled
HTTP 204
: Means that something was found, but it's empty.
HTTP 404
: Not found.
If your findById
always returns exactly 0 or 1 items, and this is your request:
GET /api/find-by-id/{id}
Then I believe that the result should be 404 if an item does not exist.
204
is different. It doesn't really imply an item doesn't exist, it implies that the operation was a success, but the result is empty + a hypermedia client should not refresh the current view. It's rarely used as a response to GET
request because it doesn't make that much sense.
Since it sounds like you're using a database, the equivalent to 204
/ empty would be a database record with that id, but all the fields are null
. The equivalent to 404
would be that the database record does not exist at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With