CodecConfigurationException при сохранении ZonedDateTime в MongoDB с Spring Boot> = 2.0.1.RELEASE - PullRequest
0 голосов
/ 06 октября 2018

Мне удалось воспроизвести мою проблему с минимальным изменением официального руководства Spring Boot для Доступ к данным с MongoDB , см. https://github.com/thokrae/spring-data-mongo-zoneddatetime.

После добавления поля java.time.ZonedDateTime вкласс Customer, выполняющий пример кода из руководства, завершается с ошибкой CodecConfigurationException:

Customer.java:

    public String lastName;
    public ZonedDateTime created;

    public Customer() {

вывод:

...
Caused by: org.bson.codecs.configuration.CodecConfigurationException`: Can't find a codec for class java.time.ZonedDateTime.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) ~[bson-3.6.4.jar:na]
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) ~[bson-3.6.4.jar:na]
at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) ~[bson-3.6.4.jar:na]

Это можно решитьизменив версию Spring Boot с 2.0.5.RELEASE на 2.0.1.RELEASE в pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

Теперь исключение прошло, и объекты Customer, включая поля ZonedDateTime записано в MongoDB .

Я подал ошибку ( DATAMONGO-2106 ) в проекте spring-data-mongodb, но поймет, что изменение этого поведения нежелательно и не имеет высокогоприоритет.

Какой лучший обходной путь?При получении утки сообщения об исключении я нахожу несколько подходов, таких как регистрация настраиваемого кодека , настраиваемого преобразователя или использование Jackson JSR 310 .Я бы предпочел не добавлять пользовательский код в мой проект для обработки класса из пакета java.time.

1 Ответ

0 голосов
/ 01 ноября 2018

Сохранение типов даты и времени с часовыми поясами никогда не поддерживалось Spring Data MongoDB, как заявил сам Оливер Дротбом в DATAMONGO-2106 .

Это известные обходные пути:

  1. Используйте тип даты и времени без часового пояса, например, java.time.Instant.(Обычно рекомендуется использовать UTC только в бэкэнде, но мне пришлось расширить существующую кодовую базу, которая следовала другому подходу.)
  2. Написать собственный преобразователь и зарегистрировать его, расширив AbstractMongoConfiguration.Смотрите рабочий ветвь converter в моем тестовом репозитории.

    @Component
    @WritingConverter
    public class ZonedDateTimeToDocumentConverter implements Converter<ZonedDateTime, Document> {
        static final String DATE_TIME = "dateTime";
        static final String ZONE = "zone";
    
        @Override
        public Document convert(@Nullable ZonedDateTime zonedDateTime) {
            if (zonedDateTime == null) return null;
    
            Document document = new Document();
            document.put(DATE_TIME, Date.from(zonedDateTime.toInstant()));
            document.put(ZONE, zonedDateTime.getZone().getId());
            document.put("offset", zonedDateTime.getOffset().toString());
            return document;
        }
    }
    
    @Component
    @ReadingConverter
    public class DocumentToZonedDateTimeConverter implements Converter<Document, ZonedDateTime> {
    
        @Override
        public ZonedDateTime convert(@Nullable Document document) {
            if (document == null) return null;
    
            Date dateTime = document.getDate(DATE_TIME);
            String zoneId = document.getString(ZONE);
            ZoneId zone = ZoneId.of(zoneId);
    
            return ZonedDateTime.ofInstant(dateTime.toInstant(), zone);
        }
    }
    
    @Configuration
    public class MongoConfiguration extends AbstractMongoConfiguration {
    
        @Value("${spring.data.mongodb.database}")
        private String database;
    
        @Value("${spring.data.mongodb.host}")
        private String host;
    
        @Value("${spring.data.mongodb.port}")
        private int port;
    
        @Override
        public MongoClient mongoClient() {
            return new MongoClient(host, port);
        }
    
        @Override
        protected String getDatabaseName() {
            return database;
        }
    
        @Bean
        public CustomConversions customConversions() {
            return new MongoCustomConversions(asList(
                    new ZonedDateTimeToDocumentConverter(),
                    new DocumentToZonedDateTimeConverter()
            ));
        }
    }
    
  3. Напишите собственный кодек.По крайней мере, в теории.Моя ветвь тестирования кодека не может разархивировать данные при использовании Spring Boot 2.0.5 при нормальной работе с Spring Boot 2.0.1.

    public class ZonedDateTimeCodec implements Codec<ZonedDateTime> {
    
        public static final String DATE_TIME = "dateTime";
        public static final String ZONE = "zone";
    
        @Override
        public void encode(final BsonWriter writer, final ZonedDateTime value, final EncoderContext encoderContext) {
            writer.writeStartDocument();
            writer.writeDateTime(DATE_TIME, value.toInstant().getEpochSecond() * 1_000);
            writer.writeString(ZONE, value.getZone().getId());
            writer.writeEndDocument();
        }
    
        @Override
        public ZonedDateTime decode(final BsonReader reader, final DecoderContext decoderContext) {
            reader.readStartDocument();
            long epochSecond = reader.readDateTime(DATE_TIME);
            String zoneId = reader.readString(ZONE);
            reader.readEndDocument();
    
            return ZonedDateTime.ofInstant(Instant.ofEpochSecond(epochSecond / 1_000), ZoneId.of(zoneId));
        }
    
        @Override
        public Class<ZonedDateTime> getEncoderClass() {
            return ZonedDateTime.class;
        }
    }
    
    @Configuration
    public class MongoConfiguration extends AbstractMongoConfiguration {
    
        @Value("${spring.data.mongodb.database}")
        private String database;
    
        @Value("${spring.data.mongodb.host}")
        private String host;
    
        @Value("${spring.data.mongodb.port}")
        private int port;
    
        @Override
        public MongoClient mongoClient() {
            return new MongoClient(host + ":" + port, createOptions());
        }
    
        private MongoClientOptions createOptions() {
            CodecProvider pojoCodecProvider = PojoCodecProvider.builder()
                    .automatic(true)
                    .build();
    
            CodecRegistry registry = CodecRegistries.fromRegistries(
                    createCustomCodecRegistry(),
                    MongoClient.getDefaultCodecRegistry(),
                    CodecRegistries.fromProviders(pojoCodecProvider)
            );
    
            return MongoClientOptions.builder()
                    .codecRegistry(registry)
                    .build();
        }
    
        private CodecRegistry createCustomCodecRegistry() {
            return CodecRegistries.fromCodecs(
                    new ZonedDateTimeCodec()
            );
        }
    
        @Override
        protected String getDatabaseName() {
            return database;
        }
    }
    
...