我有一个Spring Boot应用程序和以下DTO
类:
public class CarDto {
private Map<Instant, List<CarModelDto>> dateToCarModels = new HashMap<>();
// getters/setters omitted for brevity
}
当我从postman
拨打电话时,它看起来像这样:
{
"dateToCarModels": {
"1544612555": [{
<obj1>
},
{
<obj2>
}],
"1544785355": [{
<obj1>
}]
}
}
它给我以下错误:
JSON parse error: Cannot deserialize Map key of type
`java.time.Instant` from String "1544612555": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text
'1544612555' could not be parsed at index 0; nested exception is
com.fasterxml.jackson.databind.exc.InvalidFormatException: (...)
这是可以理解的,因为我有Instant
作为键映射(并在JSON中传递String
-因为JSON键必须是字符串)。
是否有比writting own key serializer/deserializer更好的解决方法?