简体   繁体   中英

REST HAL Best Practice: Should I use links or nested JSON object when POSTing?

I would like to create a new Employee that reports to a Manager . An Employee cannot be POSTed without a Manager . Provided that I already have the Manager object and the URI for the Manager , is it a better practice to POST a Manager object nested:

{
    "firstname": "John",
    "lastname": "Smith",
    "manager": {"id":123, "firstname": "Albert", "lastname": "Doe"}
}

or using a link to manager:

{
    "firstname": "John",
    "lastname": "Smith",
    "_links" : {
      "manager" : {
        "href" : "http://localhost:8080/api/manager/123"
      }
    }
}

Does HAL require a link to the manager?

Note: I'm using Spring Data Rest (which uses HAL) and Spring HATEOAS. I found that @RestResource(exported = false) is required if POSTing with a nested object.

How about just posting manager's id? Your JSON would look like:

{
    "firstname": "John",
    "lastname": "Smith",
    "managerId": 123
}

And in your logic before creating your Employee you can check if the manager exists and fetch it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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