我正在使用RestKit的RKClient进行多部分图像数据和一些值的发布,如下所示:
RKParams* params = [RKParams params];
[params setValue:foo.accountId forParam:@"accountId"];
[params setValue:foo.identifier forParam:@"fooId"];
[params setValue:_photoId forParam:@"photoId"];
[params setData:data MIMEType:@"image/png" forParam:@"image"];
[[RKClient sharedClient] post:@"/foo/uploadPhoto" params:params delegate:self];
这很好用,我的后端服务器响应服务器端模型对象的JSON表示,它看起来像这样:
{"id":"4ee2b4670364720c089e75b9","accountId":"4ebee3469ae2d8adf983c561","fooId":"4ec0983d036463d900841f0b","photoId":"E5B20AF1-9F10-4175-8262-852BDA3DEDE9","filename":"4ebee3469ae2d8adf983c561_4ec0983d036463d900841f0b_E5B20AF1-9F10-4175-8262-852BDA3DEDE9","contentType":"image/png"}
我现在需要做的是将其映射到我的客户端(iOS)模型对象。客户端模型对象几乎相同,但不完全相同(因此使用RKJSONParser的objectFromString方法不是一个选项),因此我定义了一个自定义RKObjectMapping来处理映射。 RKClient的委托只获取RKResponse,那么如何使用响应和mapper来获取客户端模型对象的实例呢?
注意:要清楚,我非常熟悉使用RKObjectManager发布对象和映射响应时的工作原理。我的情况的独特部分是我使用RKClient来实现多部分帖子。不幸的是,RKClient似乎没有像RKObjectManager那样可以处理响应映射的简单方法......除非我遗漏了某些东西(我希望并且我都会为你指出这一点;)。
答案 0 :(得分:1)
好吧,this post was similar(但是没有功能),它给了我一些在RKObjectLoader上使用这个方法的新技术的想法
- (RKObjectLoader *)postObject:(id<NSObject>)object delegate:(id<RKObjectLoaderDelegate>)delegate block:(void ( ^ ) ( RKObjectLoader *))block
所以现在我可以获得映射的好处,如何使用RKClient并不明显。
路由器设置:
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:kApiUrlBase];
[objectManager.router routeClass:[PAPetPhoto class] toResourcePath:@"/pet/uploadPhoto" forMethod:RKRequestMethodPOST];
映射设置:
RKObjectMapping *papetPhotoMapping = [RKObjectMapping mappingForClass:[PAPetPhoto class]];
[papetPhotoMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[papetPhotoMapping mapAttributes:@"accountId", @"petId", @"photoId", @"filename", @"contentType", nil];
[objectManager.mappingProvider addObjectMapping:papetPhotoMapping];
[objectManager.mappingProvider setSerializationMapping:[papetPhotoMapping inverseMapping] forClass:[PAPetPhoto class]];
[objectManager.mappingProvider setMapping:papetPhotoMapping forKeyPath:@"petPhoto"];
帖子:(注意,因为我在块中构建了所有参数,我的对象只是一个虚拟实例来触发正确的路由和映射器。)
PAPetPhoto *photo = [[PAPetPhoto alloc] init];
[[RKObjectManager sharedManager] postObject:photo delegate:self block:^(RKObjectLoader *loader){
RKParams* params = [RKParams params];
[params setValue:pet.accountId forParam:@"accountId"];
[params setValue:pet.identifier forParam:@"petId"];
[params setValue:_photoId forParam:@"photoId"];
[params setValue:_isThumb ? @"THUMB" : @"FULL" forParam:@"photoSize"];
[params setData:data MIMEType:@"image/png" forParam:@"image"];
loader.params = params;
}];
服务器端点(Java,Spring MVC)
@RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> handleFormUpload(@RequestParam("accountId") String accountId,
@RequestParam("petId") String petId,
@RequestParam("photoId") String photoId,
@RequestParam("photoSize") PhotoSizeEnum photoSize,
@RequestParam("image") Part image) throws IOException {
if (log.isTraceEnabled())
log.trace("uploadPhoto. accountId=" + accountId + " petId=" + petId + " photoId=" + photoId + " photoSize=" + photoSize);
PetPhoto petPhoto = petDao.savePetPhoto(accountId, petId, photoId, photoSize, image);
Map<String, Object> map = GsonUtils.wrapWithKeypath(petPhoto, "petPhoto");
return map;
}
服务器响应JSON (请注意与映射设置对应的“petPhoto”的keyPath):
{
petPhoto = {
accountId = 4ebee3469ae2d8adf983c561;
contentType = "image/png";
filename = "4ebee3469ae2d8adf983c561_4ec0983d036463d900841f09_3FED4959-1042-4D8B-91A8-76AA873851A3";
id = 4ee2e80203646ecd096d5201;
petId = 4ec0983d036463d900841f09;
photoId = "3FED4959-1042-4D8B-91A8-76AA873851A3";
};
}
<强>代表:强>
- (void) objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
if ([objectLoader wasSentToResourcePath:@"/pet/uploadPhoto"]) {
PAPetPhoto *photo = (PAPetPhoto*)object;
}
}