嵌入式HAL资源集合中没有自我href

Wil*_*ilt 3 collections self href embedded-resource hal-json

我使用self href客户端的from my HAL资源来查找CRUD操作的正确路径.在单个(吨)资源中,这工作正常(请参阅下面的地址资源,_links包含self href嵌入资源中包含的地址资源)但是当归结为集合时,这是一个不同的故事.该_links集合时是一家集都没有呈现_embedded.

之前我通过阅读第一个孩子的网址来解决这个问题.但这还不够.如果集合是空的,我只有一个空数组,不可能像这样提取url.如果我想在集合中创建一个新项目,我希望我的客户端POST通过阅读self href来自知道在哪里发送数据_links._links像这样包含我的收藏是一个好主意:

{
    "_links": {
        "self": {
            "href": "http://example.com/api/v1/users/1"
        }
    },
    "_embedded": {
        "contacts": {
Run Code Online (Sandbox Code Playgroud)

现在我可以在这里访问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"
}
Run Code Online (Sandbox Code Playgroud)

编辑(一年后)

最后,我总是通过将嵌入资源的链接添加到父资源来解决这个问题.所以在上面的例子中,我的响应对象看起来像这样:

{
    "_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"
}
Run Code Online (Sandbox Code Playgroud)

所以无论我是否嵌入了资源,我总是知道它们的位置.对于联系人集合,我将链接到_links数组中的我的集合端点以及联系人本身_embedded.

Jon*_*n W 5

是.这样做不仅是好习惯; 它是由HAL规范推荐的:

每个资源对象应该包含一个"自我"链接,该链接与IANA注册的"自我"关系(由RFC5988定义)相对应,其目标是资源的URI.

资源集合本身就是一种资源,不要忘记.