2

Im implementing a hal/json api.

The root object (soldier) has a related object (sword), that is mapped under /soldier/{object_id}/sword

{
  "id": 12345,
  "name": "Sir SmokeALot",
  "_links": {
    "self": {
      "href": "http://soldier.local:8080/soldier/12345"
    },
    "sword": {
      "href": "http://soldier.local:8080/soldier/12345/sword"
    }
  }
}

But not every soldier has a sword. Imagine the poor soldier without a sword.

  1. if i retrieve the soldier-object, should the link /soldier/{object_id}/sword appear in the _links-array?

  2. And if it should, what would i get back from this link? An empty object or a 404? Is it even allowed by hal/json RFC to have links in the _links-array that lead to a 404?

I don't ask how to solve this, i want to know whats right.

cari
  • 2,251
  • 2
  • 18
  • 27

1 Answers1

1

Ideally, I think you wouldn't even publish the link if a soldier doesn't have a sword (if it's not expensive to calculate). Returning HttpStatusCode 404 leaves no room for interpretation in my opinion, unlike an "empty object" (whatever that would be). I also don't see any issues with a link leading to a 404, but the proposed draft specification might be interpreted otherwise (RFC 5988 also seems to have info on this).

I don't think you'll find very strict, strongly enforced rules in the area of RESTful HAL, more like guidelines, there's rarely right or wrong, black or white, more like shades of grey - just find out what works for you and establish a clear contract for your clients.

Community
  • 1
  • 1
Alexandru Marculescu
  • 5,569
  • 6
  • 34
  • 50
  • 1
    well, its not that easy to not publish a link, since then you dont know that this object _may_ exist, and you cant store something there without that link. The default behaviour of Spring Data Rest seems to be giving back a 404 link in this situation. – cari Nov 11 '16 at 08:27