这个RESTful JSON响应格式是否符合HATEOAS?
使用REST API进行工作,遇到了一个问题,我想传递一个代表关系的值,但也是该关系的URL,以便它可以与HATEOAS兼容。
我想我已经提出了一个合适的解决方案,但希望得到那些有更多知识的人的确认。
这个RESTful JSON响应是否仍然符合HATEOAS原则?
{
"employee":{
"empId":12345,
"fName":"Bubba",
"lName":"Gump",
"title":"Shrimp",
"reportsTo":54321,
"hateoas":{
"self":"http://www.bubbagumpshrimp.com/rest/Employees/12345",
"reportsTo":"http://www.bubbagumpshrimp.com/rest/Employees/54321",
"directReports":"http://www.bubbagumpshrimp.com/rest/Employees/?reportsTo=12345"
}
}
}
那么你们都在想什么? 这种格式会起作用吗?
根据以下@fumanchu的建议,这是我现在尝试使用的格式...
{
"employee":{
"empId":12345,
"fName":"Bubba",
"lName":"Gump",
"title":"Shrimp",
"reportsTo":54321,
"hateoas":{
"collection":"http://www.bubbagumpshrimp.com/rest/Employees/",
"self":"12345",
"reportsTo":"54321",
"directReports":"12345/DirectReports"
}
}
}
感谢您的指导!
它“起作用”,但它是多余的。 一旦你有了这个URI,为什么要保留那些没有提及它们的语义或者它们如何被使用的裸ID呢? 我建议你试试这个:
{
"employee":{
"self":"http://www.bubbagumpshrimp.com/rest/Employees/12345",
"fName":"Bubba",
"lName":"Gump",
"title":"Shrimp",
"reportsTo":"http://www.bubbagumpshrimp.com/rest/Employees/54321",
"directReports":"http://www.bubbagumpshrimp.com/rest/Employees/12345/directReports"
}
}
(没有理由将“directReports”资源公开为“?reportsTo = 12345”,最好是通过它的含义来识别它,而不是它的实现。)
如果你控制你的API和/或媒体类型(并且你必须告诉你的客户在哪里期待URI,因为JSON没有定义任何),你甚至可以通过声明“reportsTo”来缩短它,和“directReports”值是相对于“self”的URI:
{
"employee":{
"self":"http://www.bubbagumpshrimp.com/rest/Employees/12345",
"fName":"Bubba",
"lName":"Gump",
"title":"Shrimp",
"reportsTo":"54321",
"directReports":"12345/directReports"
}
}
链接地址: http://www.djcxy.com/p/41019.html
上一篇: Would this RESTful JSON response format be compliant with HATEOAS?
下一篇: RESTful API runtime discoverability / HATEOAS client design