在设计Web API时,我总是想知道应该在响应中呈现实体的深度;意味着如果另一个实体与查询的资源相关,那么最顶层的实体是否应仅包括ID的引用或完整的结构。例如,仅使用id引用,然后使用id查询实际实体:
请求
GET HTTP/1.1
/students/<id_student>/exams
响应
[ 1, 45, 65 ]
请求
GET HTTP/1.1
/exams/1
响应
{
"id": 65,
"takenOn": "2012-04-23T18:25:43.511Z",
"score": 95
}
请求
GET HTTP/1.1
/exams/1
响应
{
"id": 65,
"takenOn": "2012-04-23T18:25:43.511Z",
"score": 95
}
或在第一个回复中包含更多详细信息:
请求
GET HTTP/1.1
/students/<id_student>/exams
响应
[
{
"id": 65,
"takenOn": "2012-04-23T18:25:43.511Z",
"score": 95
},
{
"id": 44,
"takenOn": "2012-04-23T18:25:43.511Z",
"score": 85
},
]
那么根据您的经验,我们会建议什么?
提前致谢。