Loopback Android SDK 中的嵌套模型

Nested Models in Loopback Android SDK

我有一个带有 MongoDB 后端的环回实例,并且定义了一个模型,该模型具有一个名为 'location' 的嵌套(匿名)模型作为 属性:

  "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" 模型提取到 thing.java class 的实例中,该 thing.java class 从环回模型 class.

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 */
  }

当我从服务器获取所有 "things" 时,它们看起来都很好,除了嵌套的 "location" 总是空的。我已经验证我可以从 loopback 的 REST api 中获取事物,并且位置正确填充。但似乎环回客户端不会反序列化和填充 'location'。 "Location" 是一个简单的 java class,只有 2 个整数(纬度和经度)。我也尝试让它从 'Model' 扩展,但它仍然返回 null。

public class Location extends Model {
    private String lat;
    private String lng;
    /* getters/setters removed */
}

有什么想法吗?

This functionnality does not seem to be supported at the moment.

We should hopefully get this for free once we replace our hand-written JSON parser with a proper library supporting custom annotations

同时,no-brainer 解决方案是创建一个 location 模型,添加一个 thing HasOne location 关系,然后使用 "api/thing/{id}/location" 端点。

或者您可以分叉 the project,更改 JSON 解析库并提出拉取请求。由你决定 ;)

编辑:或者,您可能已经想到了,但更简单的解决方案是删除嵌套对象并使用两个经典属性 location_lat location_lng

改了几行sdk,现在可以递归解析所有嵌套模型了

compile 'com.github.farruxx:loopback-sdk-android:v1.6.3'