java 无法从 DynamoDB 加载数据,因为 List<Object> 类型的属性无法取消转换并使用 @DynamoDBDocument 保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45371805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:40:18  来源:igfitidea点击:

Cannot load data from DynamoDB due to an attribute of type List<Object> which is not able to get unconverted and was saved using @DynamoDBDocument

javaamazon-web-servicesamazon-dynamodb

提问by Manthan Jamdagni

I am trying to save a List<CustomObject>using the @DynamoDBDocument but it gives me a DynamoDBMappingException : could not unconvert attribute.

我正在尝试List<CustomObject>使用 @DynamoDBDocument保存一个,但它给了我一个 DynamoDBMappingException : could not unconvert 属性。

Here is what my Entity class looks like -

这是我的实体类的样子 -

@lombok.Data
@DynamoDBTable(tableName = "carTable")
public class Car {
   @DynamoDBHashKey(attributeName = "name")
   private carName;

   @DynamoDBRangeKey(attributeName = "model")
   private carModel;

   @DynamoDBAttribute(attributeName = "manufacturers")
   private List<Manufacturer> manufacturers;
}

The Manufacturer class looks like -

制造商类看起来像 -

@lombok.Data
@DynamoDBDocument
public class Manufacturer {
    @DynamoDBAttribute
    private String manufacturerName;
}

When using this entity and saving the values into the table it saves properly as JSON, but when retrieving it, an exception is thrown -

使用此实体并将值保存到表中时,它会正确保存为 JSON,但在检索它时,会引发异常 -

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Car[manufacturers]; could not unconvert attribute
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:271)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:456)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:422)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:433)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.load(AbstractDynamoDBMapper.java:85)

What am I missing here, do I need to add some sort of TypeConverter or Marshaller here?

我在这里错过了什么,我需要在这里添加某种类型转换器或 Marshaller 吗?

回答by Manthan Jamdagni

A no-args constructer was required to unconvert the attribute, adding @lombok.NoArgsConstructor to the Manufacturer class solved my problem.

需要一个无参数构造函数来取消转换属性,将 @lombok.NoArgsConstructor 添加到制造商类解决了我的问题。

回答by Ivan Mushketyk

I don't think you need a custom converter here, it looks like that DynamoDB has some issues with converting DynamoDB item into a Java object.

我认为您在这里不需要自定义转换器,看起来 DynamoDB 在将 DynamoDB 项目转换为 Java 对象方面存在一些问题。

Check:

查看:

  • Data in DynamoDB matches your object fields. Check if types are matching as well

  • What if you define setters explicitly? Maybe Lombok is a culprit here?

  • DynamoDB 中的数据与您的对象字段相匹配。检查类型是否也匹配

  • 如果您明确定义 setter 会怎样?也许龙目岛是这里的罪魁祸首?

Also it is odd that you do not specify a table name here:

同样奇怪的是,您没有在此处指定表名:

@DynamoDBTable(tableName)
public class Car {
...
}

回答by Rahul Midha

In my case, the issue was that the setters were missing

就我而言,问题是缺少二传手

This was because I was using Lombok with @Builder and @Getter annotations only

这是因为我只使用带有 @Builder 和 @Getter 注释的 Lombok

回答by Sichen Zhao

If you use @Builder, you can still use @Tolerate to add default Constructor

如果您使用@Builder,您仍然可以使用@Tolerate 添加默认构造函数

回答by HelpMatters

In my case, my java class had an attribute as string, whereas dynamodb was storing it as a number. When I changed my attribute to be a intinstead of a string, it worked smoothly.

在我的例子中,我的 java 类有一个作为字符串的属性,而 dynamodb 将它存储为一个数字。当我将我的属性更改为int而不是string 时,它运行顺利。