问题描述
我有一个REST服务,并且根据正在查看的资源类型,我可以进行某些操作
I have a REST Service and depending on the type of Resource that is being viewed I have certain Operations avialable
所以Resource1
支持Operation1
和Operation2
例如:
Resource1/Operation1
Resource1/Operation2
Resource2
仅支持Operation1
例如:
Resource2/Operation1
因此,如果我在Resource2
上收到对Operation2
的呼叫(例如:Resource2/Operation2
),则由于Operation2有效,服务器将不会引发错误,但是内部不支持该错误,因此我应该返回一个404-未找到,还是501-未实现会更准确,还是出现其他错误代码?
So if I receive a call for Operation2
on Resource2
(eg: Resource2/Operation2
) the server will not raise an error as Operation2 is valid, but internally I don't support it, so should I return a 404 - Not Found or would a 501 - Not Implemented be more accurate or another error code?
推荐答案
您必须考虑资源(存在并对其进行操作)而不是服务(如WebServices)(您称其做某事).
You have to think in terms of Resources (that exist and you operate on them) and not Services as in WebServices (which you call to do something).
因此,REST API的每个 Operation 本身都是一种资源(即,对长时间运行的操作或通过发送 POST 触发的某种更复杂的事务进行建模)对操作资源的请求)-并且该资源的URI是任何不透明的字符串,可以是'Resource1/Operation1'或"/someOther/arbitrary/path",
So every Operation of your REST API is a resource by itself (i.e. to model long running operations or some sort of more complex transaction that are triggered by sending a POST request to the operation resource) - and the URI to that resource is any opaque string, which can be 'Resource1/Operation1' or "/someOther/arbitrary/path",
A 501表示HTTP服务器本身具有未实现的,必须满足请求的特定功能-即它缺乏某些技术功能-但资源本身是有效的.
A 501 means the HTTP server itself has a particular feature not implemented that is require to fulfill the request - i.e. it lacks some technical features - but the resource itself is valid.
但是,正如您所写的那样,您的Rest API根本不支持Resource2的Operation2,这意味着不存在诸如"Resource2/Operation2"服务资源之类的东西.因此404响应是唯一合理的选择.
But as you've written, your Rest API is not designed to support Operation2 for Resource2 at all, which means that there is no such thing as a "Resource2/Operation2" service resource. Therefore 404 response is the only reasonable choice.
在客户端将如何交互..."方面,问自己:鉴于您是资源(即某个任意网站)的使用者,在接收时您是否会重新访问相同的URL 404? (可能不会)(如果没有),您会收到501吗? (可能是的,因为您认为问题已经解决了)
In terms of "how client will interact...", ask yourself: given you are the consumer of a resource (i.e. some arbitrary web site), will you revisit the same URL when receiving a 404? (probably not) will you do when receiving a 501? (probably yes, as you assume the problem has been resolved in the meantime)
TL; DR
返回404
这篇关于REST中的资源不支持操作时的HTTP状态代码404或501的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!