未找到 RESTful 资源.404 还是 204?Jersey 在从处理程序返回 null 时返回 204 [英] RESTful resource not found. 404 or 204? Jersey returns 204 on null being returned from handler

查看:24
本文介绍了未找到 RESTful 资源.404 还是 204?Jersey 在从处理程序返回 null 时返回 204的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在寻找 /Resource/Id 并且该资源不存在,我一直认为 404 是适当的响应.但是,当从 Jersey 处理程序返回 null 时,我会返回204 No Content".我可能会与其中任何一个合作,但很好奇其他人对此的想法.

If you are looking for /Resource/Id and that resource does not exist, I had always though that 404 was the appropriate response. However, when returning null from a Jersey handler, I get back a "204 No Content". I can likely work with either one, but am curious to others thoughts on this.

回答我自己的下一个问题.要让 Jersey 返回 404,您必须抛出异常.

To answer my own next question. To get Jersey to return 404 you must throw an exception.

    if (a == null)
        throw new WebApplicationException(404);

推荐答案

HTTP代码定义 说明以 2 开头的代码表示调用成功,4 表示调用不成功.

The HTTP Code Definition states that the codes beginning with 2 are for successful calls and 4 for unsuccessful ones.

  • 当您收到 204 时,它只是向您显示没有可返回的内容(通常您希望进行不需要返回任何内容的 POST 或 PUT 调用)

  • When you get the 204 it just shows you that there is nothing to return (usually you expect if you make a POST or PUT call that does not need to return anything)

当您收到 404 时,它表明调用不成功,因此它不会返回任何内容.

When you get a 404 it states that the call was unsuccessful, so it will not return anything.

在您的情况下,404 是合适的,而不是 204!因为您可能想对来电者说他打错了电话.

In your situation the 404 is appropriate, not the 204! Since you probably want to say to the caller that he made a wrong call.

这篇关于未找到 RESTful 资源.404 还是 204?Jersey 在从处理程序返回 null 时返回 204的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆