【发布时间】:2018-01-11 10:53:33
【问题描述】:
您好 REST HATEOAS 专家!
这是来自 HATEOAS 服务的典型 JSON 响应
{
links: {
'add': 'http://myhost/API/students',
'csv-export': 'http://myhost/API/students/export'
},
list: [
{id: '1', name: 'John Doe', links: {'see': 'http://myhost/API/students/1'},
{id: '2', name: 'Jane Doe', links: {'see': 'http://myhost/API/students/2'},
...
]
}
从这里我们可以看到发送完整的 HTTP(s) 链接以及响应是
- 非常占用空间(是的,我知道 gzip)
- 难以阅读(想象一下当列表中有 1000 个元素时)
- 还不够,因为您仍然不知道使用哪种方法(也不知道需要发送哪些内容)
所以我正在做的事情如下:
{
resType: 'studentsCollection',
rels: ['add','csv-export'],
list: [
{id: '1', name: 'John Doe', resType: 'studentCollectionItem', rels: ['see'],
{id: '2', name: 'Jane Doe', resType: 'studentCollectionItem', rels: ['see'],
...
]
}
我提供了额外的端点: /resTypes/studentsColelction/rels
{
resType: 'studentsCollection',
links: {
'add': {method: 'POST', url: '/students', contentType: 'studentForm', resultType: 'studentId'},
'csv-export': {method: 'GET', url: '/students/export', contentType: 'studentCriteria', resultType: 'binary'}
}
}
/resTypes/studentCollectionItem/rels { resType: 'studentCollectionItem', 链接:{ 'see': {method: 'GET', url: '/students/{id}', resultType: 'studentEntity'} } }
还有 /resTypes 一次返回所有 resTypes 以避免多次往返
【问题讨论】:
标签: hateoas