Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDBMappingException on mapping Map<String, Object>attribute

I am trying to save the instance of the class bellow into dynamdb but getting DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted exception.

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

    @DynamoDBHashKey
    private String orderId;

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

    ....... 
}

It fails during map conversion, seems the problem is in Object generic type.

could someone help, where is the problem here or maybe SDK doesn't support such kind of conversion ?

Thanks!

like image 766
Aram Mkrtchyan Avatar asked Oct 16 '25 01:10

Aram Mkrtchyan


1 Answers

DynamoDB won't know how to convert the objects in the Map<,>, you'll have to create a custom type converter. once you've done this you can annotate the prooperty with @DynamoDBTypeConverted(converter = xxx):

In your example:

@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;
    }
}

More information can be found here

like image 152
Kevin Smith Avatar answered Oct 19 '25 04:10

Kevin Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!