0

我有一个用例,我想从 Java 中的 DynamoDB 读取地图。我面临的问题是如何将 Map 转换为 Java 中的 POJO。例如,让以下内容为 Dynamo DB 中的内容

{
    "someInfo": {
         "age" : {
            minAge : xx,
            maxAge : yy,
          },
         "city" : "abc",
         "education" : {
             "university" : "xyz",
             "major" : "def"
          }
     }
}

在我的 Java 服务中,我想将它读入一个对象,比如 Map。City、Education、Age 都是它们自己的 Java 类。如何将此数据建模为一个类?以下模型是否有效:

@DynamoDBTable(tableName = "someotherinfo")
@Getter @Setter
public class SomeOtherInfo {


    @DynamoDBHashKey(attributeName = "id")
    private Long id;

    private Map<String, Object> someInfo;
}

有没有更好的方法来做到这一点?我在想也许,我可以将 someInfo 作为 Map 并在从表中写入和读取时将对象序列化和反序列化为 JSON。这行得通吗?

4

1 回答 1

0

为什么不创建自己的SomeInfo课程:

class SomeInfo{
    private City city;
    private Age age;
    private Education education;

    // constructors

    // getters and setters
}

然后引用它而不是地图:

@DynamoDBTable(tableName = "someotherinfo")
@Getter @Setter
public class SomeOtherInfo {

    @DynamoDBHashKey(attributeName = "id")
    private Long id;

    private SomeInfo someInfo;
}
于 2019-01-15T11:01:35.573 回答