如何解决当类实现接口时,DynamoDBDocument不起作用
我目前有一个类似的实现,我正在尝试用TopLevelClass
保存表DynamoDBMapper
Public interface A {}
@DynamoDBDocument
Public class C1 implements A {
variables…
}
@DynamoDBTable
Public class TopLevelClass {
A obj;
//Getter and setter of A
}
我尝试了不使用Interfaces的情况并且工作正常,但是一旦添加Interfaces,就会抛出此异常:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: 不支持;需要@DynamoDBTyped或@DynamoDBTypeConverted
我还尝试将注释添加到接口,并引发相同的异常。
我在考虑是否可以使用转换器来解决此问题,但是我的表中有17个必须实现接口的项目,因此如果DynamoDB具有“为我们”。
是否有解决此问题的方法?
解决方法
您可以编写自己的转换器来完成与DynamoDbDocument相同的工作。
示例:
public class MyInterfaceConverter
implements DynamoDBTypeConverter<Map<String,AttributeValue>,List<MyInterface>> {
private static ObjectMapper MAPPER = new ObjectMapper();
@Override
public Map<String,AttributeValue> convert(List<MyInterface> myInterfaces) {
final Map<String,AttributeValue> map = new LinkedHashMap<>();
for (int i = 0; i < myInterfaces.size(); i++) {
MyInterfaceImplementation myInterfaceImplementation =
(MyInterfaceImplementation) myInterfaces.get(i);
try {
String json = MAPPER.writeValueAsString(myInterfaceImplementation);
Item item = Item.fromJSON(json);
AttributeValue value = new AttributeValue();
Map<String,AttributeValue> classToAttributeMap = ItemUtils.toAttributeValues(item);
value.setM(classToAttributeMap.entrySet()
.stream()
.filter(e -> e.getValue().isNULL() == null )
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue)));
map.put(Integer.toString(i + 1),value);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return map;
}
@Override
public List<MyInterface> unconvert(Map<String,AttributeValue> stringAttributeValueMap) {
// to be implemented.
return null;
}
}
转换器:
ifconfig
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。