簡體   English   中英

DynamoDBMapper for java.time.LocalDateTime

[英]DynamoDBMapper for java.time.LocalDateTime

我在我的java應用程序中使用java.time.LocalDateTime。 我也嘗試使用DynamoDBMapper並通過注釋保存LocalDateTime變量。 不幸的是我收到以下錯誤:

DynamoDBMappingException: Unsupported type: class java.time.LocalDateTime

有沒有辦法在不使用DynamoDBMarshalling情況下進行此映射?

沒有AWS DynamoDB Java SDK無法在不使用任何注釋的情況下本地映射java.time.LocalDateTime。

要執行此映射,您必須使用AWS Java SDK 1.1.120版中引入的DynamoDBTypeConverted注釋。 從此版本開始,不推薦使用注釋DynamoDBMarshalling

你可以這樣做:

class MyClass {

    ...

    @DynamoDBTypeConverted( converter = LocalDateTimeConverter.class )
    public LocalDateTime getStartTime() {

        return startTime;
    }

    ...

    static public class LocalDateTimeConverter implements DynamoDBTypeConverter<String, LocalDateTime> {

        @Override
        public String convert( final LocalDateTime time ) {

            return time.toString();
        }

        @Override
        public LocalDateTime unconvert( final String stringValue ) {

            return LocalDateTime.parse(stringValue);
        }
    }
}

使用此代碼,存儲的日期將以ISO-8601格式保存為字符串,如: 2016-10-20T16:26:47.299

盡管我說過,我發現它很簡單,可以使用DynamoDBMarshalling來編組一個字符串。 這是我的代碼片段和AWS參考

class MyClass {

    ...

    @DynamoDBMarshalling(marshallerClass = LocalDateTimeConverter.class)
    public LocalDateTime getStartTime() {
        return startTime;
    }

    ...
    static public class LocalDateTimeConverter implements DynamoDBMarshaller<LocalDateTime> {

        @Override
        public String marshall(LocalDateTime time) {
            return time.toString();
        }

        @Override
        public LocalDateTime unmarshall(Class<LocalDateTime> dimensionType, String stringValue) {
            return LocalDateTime.parse(stringValue);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM