-->

Convert a Java class to DynamoDb Map

2020-08-09 04:52发布

问题:

I have a class which contains some String and int fields. I would like to convert this class to the Map<String, AttributeValue> representation. I know that DynamoDbMapper is doing this internally, but we are not using DynamoDbMapper and I would like to know if there is any existing library that I can use to perform this conversion?

回答1:

DynamoDB has a mid-level api that you might find helpful. One of its methods is ItemUtils.toAttributeValues(Item). This method allows you to convert from an Item to an attribute value map.

Now, to get an Item, you can construct one manually (but you don’t want to) or you can construct on from a json blob using Item.fromJson(String).

Now all that remains is for you to use your favorite serializer to convert from your java data model to json. The methods I’ve mentioned seamlessly handle the rest of the conversion.

TLDR;

Pojo --> json --> Item --> Map<String, AttributeValue>