简体   繁体   中英

Mapping List<LocalDateTime> to DynamoDB

I'm trying to map to DynamoDB list of dates in Java

@DynamoDBTypeConverted(converter = LocalDateTimeConverter.class)
private List<LocalDateTime> acquisitionsDates;



public class LocalDateTimeConverter implements DynamoDBTypeConverter<String, LocalDateTime> {

    @Override
    public String convert(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }

    @Override
    public LocalDateTime unconvert(String dateTimeStr) {
        return LocalDateTime.parse(dateTimeStr);
    }
}

I have written my own converter but it works only for LocalDateTime but not for the List. Does anyone know how to do it correctly?

Should I write separate converter that will return list of strings where each string will be converted from localdatetime?

In Interface DynamoDBTypeConverter<S,T> represents S - The DynamoDB standard type, T - The object's field/property type. Use T as List.

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

    @Override
    public String convert(List<LocalDateTime> dateTime) {
        //your implementation
    }

    @Override
    public List<LocalDateTime> unconvert(String dateTimeStr) {
        //your implementation
    }
}

I wrote converter like below and it works as I wanted;)

public class ListOfLocalDateTimesConverter implements DynamoDBTypeConverter<List<String>, List<LocalDateTime>> {
@Override
public List<String> convert(List<LocalDateTime> localDateTimes) {
    return localDateTimes
            .stream()
            .map(localDateTime -> localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
            .collect(Collectors.toList());
}

@Override
public List<LocalDateTime> unconvert(List<String> strings) {
    return strings
            .stream()
            .map(str -> LocalDateTime.parse(str))
            .collect(Collectors.toList());
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM