映射Map <string,object =“”>属性时发生DynamoDBMappingException

时间:2018-07-12 07:57:13

标签: amazon-dynamodb aws-sdk

我正在尝试将贝娄类的实例保存到dynamdb中,但是 DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted例外。

@DynamoDBTable(tableName = "FulfillmentOrders")
public class FulfillmentOrder {

    @DynamoDBHashKey
    private String orderId;

    @DynamoDBAttribute
    @DynamoDBTyped(value = DynamoDBMapperFieldModel.DynamoDBAttributeType.M)
    private Map<String, Object> body;

    ....... 
}

在地图转换过程中失败,看来问题出在对象通用类型上。

有人可以帮忙吗,这里的问题出在哪里,或者SDK不支持这种转换?

谢谢!

1 个答案:

答案 0 :(得分:2)

DynamoDB将不知道如何在object中转换Map<,>,您必须创建一个自定义类型转换器。完成此操作后,您可以使用@DynamoDBTypeConverted(converter = xxx)注释属性:

在您的示例中:

@DynamoDBTable(tableName = "FulfillmentOrders")
public class FulfillmentOrder {

    @DynamoDBHashKey
    private String orderId;

    @DynamoDBAttribute
    @DynamoDBTypeConverted(converter = BodyTypeConverter.class)
    private Map<String, Object> body;
}


static public class BodyTypeConverter implements DynamoDBTypeConverter<String, Map<String, Object>> {

    @Override
    public String convert(Map<String, Object> object) {
        DimensionType itemDimensions = (Map<String, Object>) object;

        // Convert the object to a DynamoDB json string
        String json = "wibble";

        return json;
    }

    @Override
    public DimensionType unconvert(String s) {
        Map<String, Object> item = new Map<String, Object>();

        // Convert s to a Map<String, Object> here.

        return item;
    }
}

可以找到更多信息here