【发布时间】:2017-12-09 12:33:09
【问题描述】:
在我的 MySQL 数据库中,我有一个名为 Messages 的表,它有 4 行,唯一的消息 ID 为 1,2,3,4。
使用 REST Api 时,我正在访问集合资源:
http://localhost:8080/messenger/webapi/messages
我将所有消息条目都作为 JSON 获取,这是预期的; 请在下面找到:
[
{
"author": "Vineet",
"creation": "09/12/2017",
"id": 1,
"message": "Message1"
},
{
"author": "Puneet",
"creation": "09/12/2017",
"id": 2,
"message": "Message2"
},
{
"author": "Dad",
"creation": "09/12/2017",
"id": 3,
"message": "Message3"
},
{
"author": "Mom",
"creation": "09/12/2017",
"id": 4,
"message": "Message4"
}
]
使用 REST Api 时,我正在访问单个资源:
http://localhost:8080/messenger/webapi/messages/1 或 http://localhost:8080/messenger/webapi/messages/2 或 http://localhost:8080/messenger/webapi/messages/3 或 http://localhost:8080/messenger/webapi/messages/4
我得到了每个资源的预期响应,例如1
{
"author": "Vineet",
"creation": "09/12/2017",
"id": 1,
"message": "Message1"
}
问题:当我尝试访问 url 时:
http://localhost:8080/messenger/webapi/messages/9,其中 9 是数据库中永远不存在的资源。
我得到一个空白页面或最后加载的页面。和即将到来的 HTTP 代码是:204。
当“9”id 不存在时,为什么没有 404 消息? 为什么是 204?
【问题讨论】:
-
您在哪个框架中运行 REST API?如果是泽西岛,this question 可能会有所帮助。
-
@LukeWoodward 感谢您对这篇文章的帮助。但我没有得到我的答案。当资源不存在时,它在哪里查找并带来空值?应该是 404 错误,但是 204 来了。
-
你还没有回答我的问题!您是否将 Jersey 用于您的 REST API?或者您正在使用其他东西,如果是,是什么?
-
对不起。是的,我正在使用泽西岛。 @LukeWoodward