lundi 26 octobre 2015

How to handle relationships in a REST api

I am trying to get my head around how to handle relationships in REST.

I have read this thread: How to handle many-to-many relationships in a RESTful API?

If I have Drivers and Cars in my api and a Driver can only exist if connected to a Car I would make Drivers a subresource in Cars. The relationship between a Car and a Driver contains a set of properties, say averageSpeed and timeOnTheRoad. One Car can have many Drivers, but a Driver can only have one Car.

How should I add a new driver? How should I add a relationship between a driver and a car?

If I add a resource Wunderbaums which is not a subresource to Cars, but a Car can contain Wunderbaums. How should I add a relationship between a Car and a Wunderbaum?

One way of adding a relationship between two entities is to POST to /entityA/{id}/entityB/{id} and send properties for the relationship in the body. This would work for my example with Cars and Wunderbaums since Wunderbaums is not a subresource of Cars, but it would not work in my example with Cars and Drivers since it would interfere with CRUD functionality for Drivers.

Aucun commentaire:

Enregistrer un commentaire