RESTFUL API中的多对多

时间:2018-02-18 20:36:30

标签: rest web many-to-many restful-architecture

例如,假设我有员工和餐馆资源:

Employee {ID, Name, Gender, Birthday} exposed to GET\POST\PUT\DELETE
Restaurant {ID, Name, Address, Business_Number} exposed to GET\POST\PUT\DELETE

另外,我有很多资源:

Restaurant_Has_Employee {ID, Employee_ID, Restaurant_ID, Employee_Work_In_Restaurant}

所以问题是,将GET \ POST \ PUT \ DELETE公开给这个资源的最佳做法是什么?

谢谢,

1 个答案:

答案 0 :(得分:0)

我总是使用以下模式执行此操作:

parent/{id}/children

在您的情况下,如果为了示例,给定的餐馆的ID为55,那么网址GET restaurant/55/employee将返回属于该餐馆的员工列表,POST restaurant/55/employee将用于将新员工插入该餐厅的记录。 PUT在这里并不重要,因为它应该直接在employee端点上使用,但DELETE restaurant/55/employee可以用来删除属于该餐厅的所有员工。

BTW,这看起来像是this question的重复。