我正在尝试向现有的 DAO 添加 Map<String, LocalDateTime>
属性。似乎 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 中, map 最终看起来像:
{\"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 格式
关于java - 如何将 Map<String, LocalDateTime> 保存到 DynamoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58069687/