Java dynamodb文档不';类实现接口时不工作
我目前有一个类似的实现,我正试图用Java dynamodb文档不';类实现接口时不工作,java,amazon-dynamodb,Java,Amazon Dynamodb,我目前有一个类似的实现,我正试图用DynamoDBMapper Public interface A {} @DynamoDBDocument Public class C1 implements A { variables… } @DynamoDBTable Public class TopLevelClass { A obj; //Getter and setter of A } 我尝试过不使用接口,效果很好,但一旦添加接口,就会引发此异常: com.amaz
DynamoDBMapper
Public interface A {}
@DynamoDBDocument
Public class C1 implements A {
variables…
}
@DynamoDBTable
Public class TopLevelClass {
A obj;
//Getter and setter of A
}
我尝试过不使用接口,效果很好,但一旦添加接口,就会引发此异常:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:
不支持;需要@DynamoDBTyped或@DynamoDBTypeConverted
我还尝试将注释添加到接口,并引发了相同的异常
我在想,如果使用转换器,我可以克服这个问题,但是我的表有17项必须实现一个接口,所以如果DynamoDB有一个“为我们工作”的注释,我不想这样做
这个问题有解决方案吗?您可以编写自己的转换器来完成与DynamodB文档相同的工作 例如:
@DynamoDBTypeConverted(converter = MyInterfaceConverter.class)
private List<MyInterface> myInterfaces;
@DynamoDBTypeConverted(converter=MyInterfaceConverter.class)
私有列表接口;
转换器:
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;
}
}
公共类MyInterfaceConverter
实现DynamoDBTypeConverter{
私有静态ObjectMapper MAPPER=新ObjectMapper();
@凌驾
公共映射转换(列表MyInterface){
最终映射=新LinkedHashMap();
对于(int i=0;ie.getValue().isNULL()==null)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
map.put(Integer.toString(i+1),value);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
}
返回图;
}
@凌驾
公共列表取消插入(映射stringAttributeValueMap){
//有待实施。
返回null;
}
}