Search code examples
springhttpspring-hateoashateoas

PUT or PATCH for hateoas relation update


I would like understand the best practice for update foreign key with spring hateoas requests.

Is it best to use:

Url: "http://host/entity1/{{entity1_id}}/entity2" 
Method: PUT    
Body: "http://host/entity2/{{entity2_id}}"
ContentType: "text/uri-list"

Or:

Url: "http://host/entity1/{{entity1_id}}"
Method: PATCH
Body: {"entity2": "http://host/entity2/{{entity2_id}}"}
ContentType: "application/json"

Thank you all!


Solution

  • PUT is for idempotent change of the whole resource, so you have to put all the resource data. Top replace a resource you have to completely specify its characteristics.

    PATCH allows partial resource changes, by incomplete data.

    If your entity2 has more data, besides a link another entity, then PACTH is more applicable to change the foreign key only.

    See in HTTP Methods sepcification.