问题
Our team is developing RESTFul applications...we are debating the "BEST PRACTICE" approach.
Should 404 status code response be returned for a filter-like query? Say my GET URL is
.../1/service/startsWith/a.json
and it returns all values that start with A in my database...but if no "a" values are found should i just return status code 200 with an empty json string? or status code 404.
Thanks!
回答1:
See this question, in the update to my answer I address your issue. Specifically this bit,
I think the answer to whether to return 404 depends on the what is the resource that is being retrieved. Is it a representation of a search result, or is it a representation of a product? To know this you really need to look at the link relation that led us to the URL.
If the URL is supposed to return a Product representation then a 404 should be returned if the code does not exist. If the URL returns a search result then it shouldn't return a 404.
The end result is that what the URL looks like is not the determining factor. Having said that, it is convention that query strings are used to return search results so it is more intuitive to use that style of URL when you don't want to return 404s.
回答2:
It may be more sensible to return a 204 code, which means 'No Content'. This would be slightly more efficient as a 204 status cannot have any document content, plus you can detect the code instead of having to parse the response.
来源:https://stackoverflow.com/questions/4340720/rest-mapping-404-http-status-codes