collections - 嵌入式 HAL 资源集合中没有 self href

标签 collections href embedded-resource self hal-json

我在客户端使用我的 HAL 资源中的 self href 来为 CRUD 操作找到正确的路径。在单个(吨)资源中,这工作正常(请参阅下面的地址资源,包含 self href_links 包含在嵌入式资源中) 但当涉及到收藏品时,情况就不同了。当集合处于 _embedded 中时,不会呈现集合的 _links

早些时候我通过读取第一个 child 的 url 解决了这个问题。但这还不够。如果集合是空的,我只有 一个空数组,不可能像那样提取 url。如果我想在集合中创建一个新项目,我希望我的客户通过阅读 self href 知道使用 POST 将数据发送到哪里来自 _links。像这样将 _links 包含到我的收藏中是个好主意吗:

{
    "_links": {
        "self": {
            "href": "http://example.com/api/v1/users/1"
        }
    },
    "_embedded": {
        "contacts": {

现在我可以在这里访问 self href:

            "_links": {
                "self": {
                    "href": "http://example.com/api/v1/users/1/contacts"
                }
            },
            "_embedded": {
                "contacts": [
                    {
                        "_links": {
                            "self": {
                                "href": "http://example.com/api/v1/users/1/contacts/2"
                            }
                        },
                        "id": "2",
                        "name": "John Smith"
                    },
                    {
                        "_links": {
                            "self": {
                                "href": "http://example.org/api/v1/users/1/contacts/3"
                            }
                        },
                        "id": "3",
                        "name": "Jane Doe"
                    }
                ],
            }
        },
        "address": {
            "_links": {
                "self": {
                    "href": "http://example.com/api/v1/addresses/1"
                }
            },
            "street": "Bakerstreet 11",
            "postal code": "123456",
            "city": "Some city",
            "country": "Some country",
        }
    },
    "id": "1",
    "name": "John Doe"
}

编辑(一年后)

最后我通过始终将嵌入资源的链接添加到父资源来解决这个问题。所以在上面的示例中,我的响应对象将如下所示:

{
    "_links": {
        "self": {
            "href": "http://example.com/api/v1/users/1"
        },
        "contacts": {
            "href": "http://example.com/api/v1/users/1/contacts"
        },
        "address": {
            "href": "http://example.com/api/v1/addresses/1"
        }
    },
    "_embedded": {
        "contacts": [
            {
                "_links": {
                    "self": {
                        "href": "http://example.com/api/v1/users/1/contacts/2"
                    }
                },
                "id": "2",
                "name": "John Smith"
            },
            {
                "_links": {
                    "self": {
                        "href": "http://example.org/api/v1/users/1/contacts/3"
                    }
                },
                "id": "3",
                "name": "Jane Doe"
            },
        ],
        "address": {
            "_links": {
                "self": {
                    "href": "http://example.org/api/v1/addresses/1"
                }
            },
            "street": "Bakerstreet 11",
            "postal code": "123456",
            "city": "Some city",
            "country": "Some country",
        }
    },
    "id": "1",
    "name": "John Doe"
}

所以无论我是否嵌入资源,我总是知道它们位于何处。对于联系人集合,我将在 _links 数组中包含指向我的集合端点的链接,在 _embedded 中包含联系人本身。

最佳答案

是的。这样做不仅是一种好习惯;这是recommended by the HAL spec :

Each Resource Object SHOULD contain a 'self' link that corresponds with the IANA registered 'self' relation (as defined by RFC5988) whose target is the resource's URI.

资源的集合本身就是一种资源,别忘了。

关于collections - 嵌入式 HAL 资源集合中没有 self href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19400784/

相关文章:

asp.net - MVC 4 从资源设置属性值

collections - 迭代 ConcurrentQueue<T>

jquery - 使用 jQuery 修改超链接的 onclick URL 参数

javascript - 将字符串插入 url 并重定向

javascript - 链接 SVG 路径

java - Java swing 中的 gif 动画问题

.net - 在没有程序集前缀的 .NET 程序集中嵌入资源?

java - 是否可以在 Java 8 中将日志记录添加到 removeIf?

hibernate - 在 HQL 中选择集合

java - 检查 Java : which is the best method? 中的集合是否为空