用超文本应用语言(HAL)表示数据

时间:2019-08-12 08:03:41

标签: rest hateoas

在这里,当我发现Hypertext Application Language specs时,我无知地(也许是愉快地)对API进行了编码,将数据发送回去并在其上构建应用程序。这是我的两个问题?

  1. 这是推荐/官方的吗?最新版本似乎是draft specs,它已在2 1/2年前过期,并带有smattering of support in various languages。如果需要,我很乐意遵循它,因为约定很好,但是如果有更好的东西,我很乐意继续前进。我意识到这可能是一种观点问题,但是这种观点对我编写的程序很重要,因此对我的查询也很重要。

  2. 鉴于以下简单的数据方案,我如何表示返回数据?

// searching for all employees with the letters 'mil' in their names
uri = '/employees?q=mil&page=1&size=3'

res = [
    {
        _links: { self: { href: '/employees?id=3' }},
        id: 3,
        name: 'Falcione, Camila'
    },
    {
        _links: { self: { href: '/employees?id=42' }},
        id: 42,
        name: 'Fedorova, Lyudmila'
    },
    {
        _links: { self: { href: '/employees?id=48' }},
        id: 48,
        name: 'Miller, James'
    }
]

stats = {
    male: 1,
    female: 2
}

// is the following correct?
return {
    _links: {
        self: { href: '/employees?q=mil&page=1&size=3' },
        prev: { href: '' },
        next: { href: '/employees?q=mil&page=2&size=3' },
    },
    total: 3,
    stats: stats,
    _embedded: res
}

然后,进行单次记录

// getting details of one employee
uri = '/employees?id=48'

// is the following correct?
return {
    _links: { self: { href: '/employees?id=48' }},
    name: 'Miller, James',
    firstname: 'James',
    lastname: 'Miller',
    gender: 'Male',
    profession: 'oceanographer'
}

0 个答案:

没有答案