如何使用RestKit设置resource /:id

时间:2013-08-25 16:00:15

标签: restkit restkit-0.20

My Restful API:

GET http://example.com/articles
GET http://example.com/articles/:id
...

继承我的描述符设置:

RKResponseDescriptor *responseDescriptorArticleList = [RKResponseDescriptor
                                responseDescriptorWithMapping:articleMapping
                                method:RKRequestMethodGET
                                pathPattern:@"/articles"
                                keyPath:@nil     
                                statusCodes:[NSIndexSet indexSetWithIndex:200]];


RKResponseDescriptor *responseDescriptorArticle = [RKResponseDescriptor
                               responseDescriptorWithMapping:articleMapping
                               method:RKRequestMethodGET
                               pathPattern:@"/articles/:id"
                               keyPath:nil
                               statusCodes:[NSIndexSet indexSetWithIndex:200]];

(GET)http://example.com/articles的作用

[objectManager getObjectsAtPath:@"/articles"
                    parameters:nil
                    success:^sucess{...} failure:^failue{...}];

(GET)http://example.com/articles/:id的功能不起作用

//Whatever Object is article object or article.id, it doesn't work
[objectManager getObject:Object path:@"/articles/:id"
                    parameters:nil 
                    success:^sucess{...} failure:^failue{...}];

服务器控制台显示:

GET /articles/:id 404 117ms

显然它没有用article.id替换/:id

我浏览了看起来没有更新为0.20.x的RestKit文档和示例,但没有找到答案。我很感谢你的帮助。

1 个答案:

答案 0 :(得分:3)

替换并不总是在路径模式中。特别是,当调用getObject:path:parameters:success:failure: RestKit期望路径而不是路径模式时,它不会尝试替换任何东西。你的响应描述符很好,总是在那里检查路径模式。

如果提供了对象并且未提供路径(路径必须为getObject:path:parameters:success:failure:),

nil将注入路径模式。在这种情况下,对象管理器将查找与对象的类和GET请求类型匹配的路由器。因此,您需要添加路由器,例如:

[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[...Article class] pathPattern:@"/articles/:id" method:RKRequestMethodGET]];