DynamoDB在嵌套对象中转换ZonedDateTime

时间:2018-11-26 17:02:00

标签: java spring-data amazon-dynamodb

我要在项目上从MongoDB切换到DynamoDB。现在,我试图将此Post对象存储在db中。我正在使用DynamoDBTypeConverter将ZonedDateTime转换为字符串,因为DynamoDB不支持ZonedDateTime。

那很好,但是当我在Comment对象中添加ZonedDateTime字段并尝试将其转换时,它也不起作用。我尝试将一个转换器添加到Comment类,并尝试在Post类中使用该转换器进行Comment,但是似乎没有任何效果。有没有办法为DynamoDB转换嵌套对象中的字段?

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Cannot marshall type class java.time.ZonedDateTime without a custom marshaler or @DynamoDBDocument annotation.

@DynamoDBTable(tableName = "Post")
public class Post {

    @DynamoDBHashKey
    private String postNumber;
    private ZonedDateTime date;
    private List<Comment> comments;

    @DynamoDBTypeConverted(converter = ZonedDateTimeConverter.class)
    @DynamoDBAttribute
    public ZonedDateTime getDate() {
        return date;
    }

    @DynamoDBAttribute(attributeName = "comments")
    public List<Comment> getComments() {
        return comments;
    }


static public class ZonedDateTimeConverter implements DynamoDBTypeConverter<String, ZonedDateTime> {

    @Override
    public String convert(final ZonedDateTime time) {
        return time.toString();
    }

    @Override
    public ZonedDateTime unconvert(final String stringValue) {
        return ZonedDateTime.parse(stringValue);
    }
}



@DynamoDBDocument
public class Comment {

private String commentNumber;
@NotNull
private User user;
private ZonedDateTime date;

@DynamoDBTypeConverted(converter = ZonedDateTimeConverter.class)
@DynamoDBAttribute
public ZonedDateTime getDate(){
    return this.date;
}

static public class ZonedDateTimeConverter implements DynamoDBTypeConverter<String, ZonedDateTime> {

    @Override
    public String convert(final ZonedDateTime time) {
        return time.toString();
    }

    @Override
    public ZonedDateTime unconvert(final String stringValue) {
        return ZonedDateTime.parse(stringValue);
    }
}

0 个答案:

没有答案