英文:
DynamoDBDocument doesn't work when class implements interface
问题
我目前有一个类似的实现,我正在尝试使用 DynamoDBMapper
来保存 TopLevelClass
表格。
Public interface A {}
@DynamoDBDocument
Public class C1 implements A {
变量们…
}
@DynamoDBTable
Public class TopLevelClass {
A obj;
// A 的 getter 和 setter
}
我尝试过不使用接口,可以正常工作,但是一旦我添加接口,就会抛出以下异常:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:
不支持;需要 @DynamoDBTyped 或 @DynamoDBTypeConverted
我还尝试过将注解添加到接口上,但是仍然会抛出相同的异常。
我在思考是否可以通过使用转换器来解决这个问题,但是我的表格有 17 个项目,都必须实现一个接口,所以如果 DynamoDB 有一个可以“为我们完成工作”的注解,我不想走这条路。
这个问题有解决方法吗?
英文:
I currently have a similar implementation and I am trying to save the table TopLevelClass
with DynamoDBMapper
Public interface A {}
@DynamoDBDocument
Public class C1 implements A {
variables…
}
@DynamoDBTable
Public class TopLevelClass {
A obj;
//Getter and setter of A
}
I've tried without Interfaces and works fine, but once I add Interfaces this exception is thrown:
> com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:
> not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted
I also tried to add the annotation to the Interface and the same exception is thrown.
I'm thinking if with a converter I can overcome this problem, but my table have 17 items that must implement a Interface, so i didn't want to go that way if DynamoDB has an annotation that "does the work for us".
Is there a solution for this problem?
答案1
得分: 1
以下是您要翻译的内容:
您可以编写自己的转换器来执行与 DynamoDbDocument 相同的工作。
示例:
@DynamoDBTypeConverted(converter = MyInterfaceConverter.class)
private List<MyInterface> myInterfaces;
转换器:
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;
}
}
请注意,我已经将HTML编码的特殊字符进行了解码,以便在代码中正确显示。
英文:
You can write your own converter to do the same job as DynamoDbDocument.
Example:
@DynamoDBTypeConverted(converter = MyInterfaceConverter.class)
private List<MyInterface> myInterfaces;
Converter:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论