本文介绍了找不到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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:33