我有一个带有MongoDB后端的回送实例,并定义了一个模型,该模型具有一个称为“位置”的嵌套(匿名)模型作为属性:
"name": "thing",
"plural": "things",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"title": {
"type": "string",
"required": true
},
"description": {
"type": "string"
},
"location": {
"lat": {
"type": "string"
},
"lng": {
"type": "string"
}
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
在我的android项目中,我使用回送sdk将所有“物”模型提取到一个东西,该对象是从回送的模型类扩展的thing.java类的实例。
ThingRepository repository = restAdapter.createRepository(Thing.class);
repository.findAll(/* callback code ommitted */)
public class Thing extends Model {
private String id;
private String title;
private String description;
private Location location;
/* getters/setters removed */
}
当我从服务器获取所有“事物”时,它们看起来都很好,只是嵌套的“位置”始终为空。我已经验证可以从回送的REST API获取“事物”,并正确填充位置。但是,似乎回送客户端不会反序列化并填充“位置”。“位置”是一个简单的Java类,只有2个整数(纬度和经度)。我也尝试过使其从'Model'扩展,但它仍然返回null。
public class Location extends Model {
private String lat;
private String lng;
/* getters/setters removed */
}
有任何想法吗?
一旦将手写JSON解析器替换为支持自定义批注的适当库,我们就应该免费获得它
同时,您不费吹灰之力的解决方案是创建location
模型,添加thing
HasOne
location
关系并使用“ api / thing / {id} / location”端点。
或者,您可以派生项目,更改JSON解析库并发出请求。由你决定 ;)
编辑:或者,您可能已经想到了,但是一个更简单的解决方案,删除嵌套对象并使用两个经典属性 location_lat
location_lng
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句