首页 > 解决方案 > What is the correct HTTP status code for search results not found?

问题描述

We are implementing REST APIs in Java.

For GET REST endpoint, if I search for something:

  1. If any records/record found, then it will return 200 status code and results/result.

  2. If NO records found, what is the correct HTTP status code ?

When I searched in Google and SO, I found different answers like:

200 with empty list (if the API response is item but NOT list, then I can't send empty list)
204 No Content
404 Not Found

what is the correct HTTP status code if record not found or search results not found ?

标签: resthttpspring-boothttpresponse

解决方案


有两种不同的场景:

  • 要检索没有项目的资源集合(例如)的表示形式,请返回一个空数组。GET /products?name=foo200

  • 要检索不存在的单个资源(例如)的表示,请返回。GET /products/1404

例如204,状态码用于作为PUT操作的结果,允许服务器指示该操作已成功应用于目标资源。它不适合上述场景。


有关更多详细信息,请参阅此答案


推荐阅读