简体   繁体   English

HATEOAS API对象设计

[英]HATEOAS API Object Design

What is the current best practice on using pragmatic hateoas when a POST object may differ from the GET object of the same resource? 当POST对象可能与同一资源的GET对象不同时,当前使用务实的阴影的最佳实践是什么? For example, would the following be bad API design? 例如,以下内容是否会成为错误的API设计?

  1. For the resource /families a client may POST a new family and include many "members":[] in the same request. 对于资源/families ,客户可以发布一个新的家庭,并在同一请求中包括许多"members":[]

  2. /members is also a resource /members也是资源

  3. Given a successful POST or GET, the server returns an object that includes a different "family" object to include links: 给定成功的POST或GET,服务器将返回一个对象,该对象包含一个不同的“ family”对象以包含链接:

     { "id": 123, "name": "The Adams Family", "_links": { "members": { "href": "/families/123/members" } } 

I think it would be better to use nested resources in this case eg in HAL+JSON: 我认为在这种情况下使用嵌套资源会更好,例如在HAL + JSON中:

{
    "id": 123,
    "name": "The Adams Family",
    "_embedded": {
        "members": {
            "_links": {
                "self": {
                    "href": "/families/123/members"
                }
            }
        }
    }
}

I think your solution is okay as well, but you have to define the members link relation in the documentation, so the clients (and their developers) will know that "members" means family members, and not for example members of any collection (eg hydra defines a member property for collection items and schema.org defines a members property for organization members). 我认为您的解决方案也可以,但是您必须在文档中定义成员链接关系,因此客户(及其开发人员)将知道“成员”是家庭成员,而不是任何集合的成员(例如hydra为集合项定义成员属性,schema.org为组织成员定义成员属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM