DynamoDBDocument 在类实现接口时无法正常工作。

huangapple go评论138阅读模式
英文:

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&lt;MyInterface&gt; myInterfaces;

Converter:

    public class MyInterfaceConverter
implements DynamoDBTypeConverter&lt;Map&lt;String, AttributeValue&gt;, List&lt;MyInterface&gt;&gt; {
private static ObjectMapper MAPPER = new ObjectMapper();
@Override
public Map&lt;String, AttributeValue&gt; convert(List&lt;MyInterface&gt; myInterfaces) {
final Map&lt;String, AttributeValue&gt; map = new LinkedHashMap&lt;&gt;();
for (int i = 0; i &lt; 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&lt;String, AttributeValue&gt; classToAttributeMap = ItemUtils.toAttributeValues(item);
value.setM(classToAttributeMap.entrySet()
.stream()
.filter(e -&gt; 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&lt;MyInterface&gt; unconvert(Map&lt;String, AttributeValue&gt; stringAttributeValueMap) {
//                to be implemented.
return null;
}
}

huangapple
  • 本文由 发表于 2020年8月20日 18:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定