我试图将Map<String, LocalDateTime>属性添加到已经存在的DAO中。似乎AWS SDK不知道如何转换它,因为我不断收到此错误:

.DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted

我写了一个DynamoDBTypeConverter试图解决这个问题,但是它没有以正确的格式显示数据:

 public static class StringLocalDateTimeMapConverter
        implements DynamoDBTypeConverter<String, Map<String, LocalDateTime>> {
    @Override
    public String convert(Map<String, LocalDateTime> map) {
        try {
            if (map != null) {
                ObjectMapper mapper = new ObjectMapper();
                return mapper.writeValueAsString(map);
            } else {
                throw new Exception("map is empty");
            }
        } catch (Exception e) {
            LOGGER.error(String.format("Error converting map to Dynamo String. Reason - {%s}",
                    e.getMessage()));
            return "";
        }
    }

    @Override
    public Map<String, LocalDateTime> unconvert(String string) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(string, Map.class);
        } catch (Exception e) {
            LOGGER.error(String.format("Error unconverting Dynamo String to map. Reason - {%s}",
                    e.getMessage()));
            return new HashMap<>();
        }
    }
}


但是,这看起来并不理想-在DDB中,地图最终看起来像:

{\"1234567890\":{\"year\":2019,\"month\":\"SEPTEMBER\",\"monthValue\":9,\"dayOfMonth\":20,\"hour\":15,\"minute\":13,\"second\":26,\"nano\":98000000,\"dayOfWeek\":\"FRIDAY\",\"dayOfYear\":263,\"chronology\":{\"calendarType\":\"iso8601\",\"id\":\"ISO\"}}}


我不确定对数据建模的最佳方法是什么,因此DDB对此感到满意。有什么建议?

最佳答案

可能在保存到DDB之前将localDateTime解析为字符串,并确保一旦从DDB读取到它,就将其解析回LocalDateTime
您可以将其保存为ISO格式

07-26 01:02