我为什么制作这个?
RestKit 是一个伟大且设计良好的框架,当你需要与服务器 REST API 交互时它可以大大减少你的代码量。然而,服务器 REST API 需要良好的设计,响应应该整齐的结构化 JSON/XML 数据。
在我之前的一些项目中,当我必须与没有良好设计的服务器 API 交互时,编写 RestKit 映射需要的时间和直接解析原始响应的代码一样。在某些情况下,如果服务器没有一致的语言(使用不同的密钥表示相同的数据属性),情况会更糟。在这种情况下,你很可能会为每个 API 路径编写不同的对象映射。
以下是编写 RestKit 映射变得痛苦的一些原因
Jest 如何减轻你的痛苦?
我不必将我的代码与对象映射纠缠在一起,在 Jest 中,我可以将所有的映射放入 JSON 文件中。对象映射将以表达式的 JSON 对象形式编写。
以下是一个快速示例
{
"responseDescriptors": [
{
"requests": [
{
"path": "/v2/venues/search",
"keyPath": "response.venues",
"root-map-ref": "venueMap",
"statusCodes": ["2XX"]
}
],
"maps": [
{
"id": "venueMap",
"targetName": "JestExamples.Venue",
"attributes": ["name"],
"relationships" : [
{"from" : "location", "to" : "location", "map-ref" : "locationMap"},
{"from" : "stats", "to" : "stats", "map-ref" : "statsMap"}
]
},
{
"id": "locationMap",
"targetName": "JestExamples.Location",
"attributes": ["address", "city", "country", "crossStreet", "postalCode", "state", "distance", "lat", "lng"]
},
{
"id": "statsMap",
"targetName": "JestExamples.Stats",
"attributes": {
"checkinsCount" : "checkins",
"tipsCount" : "tips",
"usersCount" : "users"
}
}
]
}
]
}
Jest 和 Restkit
Jest 可以描述 Restkit 支持的基本映射
有关其他特殊情况,请参阅 Examples.md