如何处理在网页API嵌套资源(QUOT; REST")没有找到什么时候呢? [英] How to handle nested resources in Web API ("REST") when not found?

查看:142
本文介绍了如何处理在网页API嵌套资源(QUOT; REST")没有找到什么时候呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有一个资源和一个人,我有一个嵌套的资源 SomeResource 。这状态code应在下列情况下被退回,为什么?

Let say I have a resource Person and on a person I have a nested resource SomeResource. Which status code should be returned and why in the following scenarios?

/Person/1/SomeResource (SomeResource does not exist)
/Person/1/SomeResource (Person does not exist)

我觉得应该404在这两种情况下可以使用,但并非所有人都同意,我想知道这是为什么。你可以说,是底层的资源和 SomeResource 只是一个在该资源的财产,并请求 SomeResource 为此应该没有什么回报在第一个方案,但404在第二个场景。我认为这可能是一种选择,但我还是喜欢404多个。我不喜欢所有的另一种情况是与错误描述返回500,这是我在讨论中听到还有一种替代而是迫使消费者对编程我不喜欢例外。 500对我来说意味着出了问题,你真的不能做任何事情。

I think that 404 should be used in both scenarios but not everyone agrees and I would like to know why. You could argue that Person is the underlying resource and SomeResource is just a "property" on that resource and the request to SomeResource should therefor return nothing in the first scenario but 404 in the second scenario. I think that might be an option, but I still like 404 more. Another scenario that I don't like at all is to return 500 with an error description, that was an alternative I heard in the discussion as well but that forces the consumer to program against exception which I don't like. 500 for me means that something went wrong and you can't really do anything about it.

问题是,论据是,你不知道为什么你有一个404,如果你得到了它,是因为它的的不存在或者是因为该 SomeResource 不存在。

The problem is that the argument was that you don't know why you got a 404 if you got it, was it because of that the Person didn't exist or because of that the SomeResource didn't exist.

更新1:
也许我应该打出来 SomeResource 来单独的资源像

/SomeResource/1

返回像一个响应 {数据:{数据},负责人:{个人数据}} ,而这仅返回404,如果两者都缺少,但如果数据缺少200返回空数据。

which returns a response like {data: {the data}, person: {person data}}, and this only returns 404 if both are missing but if the data is missing a 200 is returned with empty data.

更新2:
我想我找到了它的地位code使用,它是400时,该人是不是因为我在这一点上已有考虑到它你永远不应该这样做的请求,那就是一个坏的请求。当 SomeResource 缺少我会去404,因为确实存在,但嵌套资源不见了。

UPDATE 2: I think I figured out which status code to use and it is 400 for when the person is not existing since I at that point considering it a request you should never have done, that is, a bad request. When SomeResource is missing I would go for 404 since the Person did exist but the nested resource was missing.

推荐答案

这可能是有益的记得,一个给定的URI的目的是确定在的的资源。语义上,一个URI结构不支持嵌套资源的概念。看来你有两个资源:&安培; SomeResource 在方案中,其中 SomeResource 具有某种与。你可以尝试这样的结构重新present这种关系:

It may be helpful to remember that a given URI is intended to identify at single "resource". Semantically, a URI structure does not support the concept of "nested" resources. It seems you have two resources: Person & SomeResource in your scenario where SomeResource has a relation of some sort with Person. You could try a structure like this to represent this relationship:

GET /person/1

{
    name: "Some Value",
    someResource: {
        "Href": "http://yourSite.com/someresource/123",
        "Title": "Get some resource for this specific person"
    },

    // other key/value pairs as appropriate...
}

这样,你不超载的400安培; 404应用特定的含义。如果客户已经收到了有效的人的结果,它只会调用 SomeResource href和将获得相应的404或不依赖于 SomeResource 123 现有。如果 URI不存在,则调用会适当返回404,因为它不存在。

This way you are not overloading the 400 & 404 with application specific meanings. If the client has received a valid Person result, it would simply invoke the SomeResource href and would receive the appropriate 404 or not depending on SomeResource 123 existing. If the Person URI does not exists, invoking it would appropriately return 404 since it does not exist.

这篇关于如何处理在网页API嵌套资源(QUOT; REST")没有找到什么时候呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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