Моя проблема, как эта CodecConfigurationException при сохранении ZonedDateTime в MongoDB с Spring Boot> = 2.0.1.RELEASE
, но я написал собственные конвертеры ZonedDateTime:
ZonedDateTimeToDateConverter
@WritingConverter
public class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
@Override
public Date convert(ZonedDateTime source) {
if (source == null) {
return null;
}
return Date.from(source.toInstant());
}
}
DateToZonedDateTimeConverter
@ReadingConverter
public class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date source) {
if (source == null) {
return null;
}
return ZonedDateTime.ofInstant(source.toInstant(), ZoneId.of("UTC"));
}
}
и мой тест:
@Autowired
ReactiveMongoOperations operations;
@Test
void test() {
ObjectId id = new ObjectId();
Document doc = new Document();
doc.append("_id", id);
// doc.append("a", ZonedDateTime.now()); // works
doc.append("zd1", new Document("f", ZonedDateTime.now())); // not working
operations.insert(doc, "test-collection").block();
Document found = Mono.from(operations.getCollection("test-collection")
.find(new Document("_id", id)).first()).block();
Assertions.assertNotNull(found);
}
, если я добавлю экземпляр ZDT в первый уровень документа, подобного этому doc.append("a", ZonedDateTime.now())
- документ сохраняется нормально. Но если я помещаю экземпляр ZDT в документ как вложенное поле (второй уровень вложенности), я получаю исключение:
org.bson.codecs.configuration.CodecConfigurationException: не удается найти код c для класса java .time.ZonedDateTime.
Что я делаю не так?