4

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!

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply Kevin! So from your answer it means that the body attribute in DynamoDb should of String type instead of Map right ? In my case it's Map type.
String is just one of many types that DynamoDB supports, as long as you map in to a supported type you'll be fine (Here's a list of supported types docs.aws.amazon.com/amazondynamodb/latest/developerguide/…)
In the case when body attribute will have String type, this solution works fine. But if the type is Map (document) a conversion fails.It seems that if I want to have a document type I should have mirroring POJO, Map<String, Object> will not work in that case.
That's because it doesn't know how to map Object to a DynamoDB type. Thus you have to tell it how to map your Object
Yes, that's the point. Values of the map can be have any DynamoDB supported type. Do you have some example for this case? Thank you Kevin!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.