问题描述
我有一个包含一些 String 和 int 字段的类.我想将此类转换为 Map<String, AttributeValue>
表示.我知道 DynamoDbMapper 在内部执行此操作,但我们没有使用 DynamoDbMapper,我想知道是否有任何现有的库可用于执行此转换?
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?
推荐答案
DynamoDB 有一个中级 API,您可能会觉得它很有帮助.它的一种方法是 ItemUtils.toAttributeValues(Item)
.此方法允许您从 Item 转换为属性值映射.
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.
现在,要获得一个项目,您可以手动构建一个(但您不想这样做),或者您可以使用 Item.fromJson(String)
.
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)
.
现在剩下的就是让您使用您最喜欢的序列化程序将您的 java 数据模型转换为 json.我提到的方法可以无缝处理其余的转换.
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>
这篇关于将 Java 类转换为 DynamoDb Map<String, AttributeValue>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!