REST / HATEOAS:将restul链接模板化为可接受的方法

时间:2013-07-22 10:16:08

标签: rest restful-architecture hateoas hypermedia

我正在调查我们正在开发的API的整个Layer3 / HATEOS / RESTful / HAL事件。

我们将公开数据列表可能会被所有这些链接臃肿。模板链接不是一个想法,这将被称为什么?我似乎无法找到它,如果它不是一个好方法,为什么?

假设一个假想的假日搜索API,入口点可以提供目的地和出发机场的列表,允许用户开始使用其中一个进行搜索。

以下内容会更有效吗? (松散地基于HAL)如果不是为什么?

{
"_links": {
    "self": {
        "href": "/"
    },
    "_templates": {
        "airport": {
            "self": { "href": "/airport/{IATA}" },
            "destinations": { "href": "destinations?airport={IATA}" },
            "parking": { "href": "/airport/{IATA}/parking"  },
            "hotels": { "href": "/hotels?airport={IATA}" },
            "directions": { "href": "/guides?airport={IATA}" },
            "search": [
                { "href": "/search?airport={IATA} title": "default" },
                { "href": "/search?airport={IATA}&type=CITY title": "citybreak" },
                { "href": "/search?airport={IATA}&type=LOW titel": "lowcost" }
            ]
        },
        "country": { 
            "self": { "href": "/destinations/{Code}/" },
            "regions": { "href": "/destinations/{Code}/regions" },
            "resorts": { "href": "/destinations/{Code}/resorts" },
            "airports": { "href": "/destinations/{Code}/airorts" },
            "search": { "href": "/search?country={Code}&region=ANY&resort=ANY" }
        }
    }
},
"_embedded": {
    "airport": [
        { "IATA": "LHR Name": "London Heathrow Airport" },
        { "IATA": "EMA Name": "East Midlands Airport" },
        { "IATA": "NWI Name": "Norwich International Airport" },
        { "IATA": "LTN Name": "London Luton Airport" },
        { "IATA": "STN Name": "London Stansted Airport" },
        { "IATA": "LCY Name": "London City Airport" },
        { "IATA": "LPL Name": "Liverpool John Lennon Airport" },
        { "IATA": "MAN Name": "Manchester Airport" },
        { "IATA": "LGW Name": "Gatwick Airport" }
    ],
    "country": [
        { "Code": "CY Name": "Cyprus" },
        { "Code": "CZ Name": "Czech Republic" },
        { "Code": "ES Name": "Spain" },
        { "Code": "FO Name": "Faroe Islands" },
        { "Code": "GG Name": "Guernsey" },
        { "Code": "GR Name": "Greece" }
    ]
}

}

1 个答案:

答案 0 :(得分:1)

这感觉就像是过早优化。我肯定永远不会为了客户端处理复杂性而交换有效负载大小...尤其是使用REST API,无论如何都要压缩HTTP 1.1 zip。

您是否真的测量过这种“更有效的方法”的好处?我的猜测是,这种方法最终会变慢,只是发送一切!