Search code examples
springspring-bootspring-hateoashateoas

HATEOAS: how to POST new child association via URI


In Spring Data Rest you can POST something like this to the table product_material which references product and material:

{
"product": "http://localhost:8080/api/products/70bdd2a0-3548-4158-b378-71850c4c1eb3",
"material": "http://localhost:8080/api/materials/1"
}

How can i implement this instead of using product's and material's id?


Solution

  • You cannot do it using hateos cause the post request will try to link these child entities using the link you provided "http://localhost:8080/api/materials/1"

    the other option is to put the whole entity, but it also will not succeed without providing the id, it will rather create a new entity.

    After issuing the "post" request, it will first search for the ID or maybe also the version, if you provided one, and if nothing found it will create a new child entity