0

I'm designing a REST api and to be as Restful as it gets I want to implement HATEOAS into the json responses.

If the linked entities are not present in database say for example every user has a passport entity but if someone doesn't have then i want to send the set of rel and href links like this:

{
"links": [
{
  "rel": "drivingLicence",
  "href": "http://localhost:9090/api/v1/drivingLicence/6"
},
{
  "rel": "passport",
  "href": null
},
{
  "rel": "self",
  "href": "http://localhost:9090/api/v1/user/4028818347818fd80147819111d60001"
}
 ],
  "userId": "4028818347818fd80147819111d60001",
  "userName": "username",
  "fullName": "Some User",
  "activeFlag": "ACTIVE",
  "createTimestamp": 1406628074000,
  "updateTimestamp": 1505697126548
}

Is there any way to achieve this using Spring HATEOAS or manually i have to check for null and then set href as null.

dinesh
  • 465
  • 1
  • 5
  • 14

1 Answers1

1

There's no way to do this with the built in Link class as it doesn't let href be null or empty (checks in constructor). Perhaps you can derive from Link and pass in non-empty href, but override the getHref to return null.

FWIW: i've never seen the href as null strategy. I've seen have the link, and if it returns 404 then that means it's not present & just not have the link. As a hypermedia client, i'd be really surprised to see href of null...but this is a new field with new practices happening all the time.

Chris DaMour
  • 3,650
  • 28
  • 37