Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodecConfigurationException when saving ZonedDateTime to MongoDB with Spring Boot >= 2.0.1.RELEASE

I was able to reproduce my problem with a minimal modification of the official Spring Boot guide for Accessing Data with MongoDB, see https://github.com/thokrae/spring-data-mongo-zoneddatetime.

After adding a java.time.ZonedDateTime field to the Customer class, running the example code from the guide fails with a CodecConfigurationException:

Customer.java:

    public String lastName;
    public ZonedDateTime created;

    public Customer() {

output:

...
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]

This can be solved by changing the Spring Boot version from 2.0.5.RELEASE to 2.0.1.RELEASE in the pom.xml:

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

Now the exception is gone and the Customer objects including the ZonedDateTime fields are written to MongoDB.

I filed a bug (DATAMONGO-2106) with the spring-data-mongodb project but would understand if changing this behaviour is not wanted nor has a high priority.

What is the best workaround? When duckduckgoing for the exception message I find several approaches like registering a custom codec, a custom converter or using Jackson JSR 310. I would prefer to not add custom code to my project to handle a class from the java.time package.

like image 484
tdkBacke Avatar asked Oct 06 '18 08:10

tdkBacke


People also ask

Does MongoDB support ZonedDateTime?

The MongoDB Converters We can handle ZonedDateTime objects (across all models) by defining a converter for reading from a MongoDB and one for writing into it.

How does spring boot store JSON objects in MongoDB?

To store raw json object/array, all you have to do is to declare the type as "Object" in the Pojo and/or DTO level on your server side. The "Object" type will work with Spring Data and MapStruct too. Then on the client side, you can send your json data as a json data.

Which is better MongoTemplate or MongoRepository?

So I'd say that MongoTemplate is a better option, unless you have a very elaborated POJO model or need the custom queries capabilities of MongoRepository for some reason. Good points/examples. However your race condition example and undesired result can be avoided using @Version to prevent that very scenario.

Does spring boot provide MongoDB auto configuration?

Spring Boot provides auto-configuration for Redis, MongoDB, Elasticsearch, Solr and Cassandra; you can make use of the other projects, but you will need to configure them yourself.

Does spring spring support MongoDB?

Spring also provides connectors like MongoTemplate and MongoRepository to perform all the database operations in MongoDB. What is Spring Boot used for? Spring Boot framework is used to create production-ready web applications with default configurations.

How to handle zoneddatetime objects in MongoDB?

We can handle ZonedDateTime objects (across all models) by defining a converter for reading from a MongoDB and one for writing into it. For reading, we're converting from a Date object into a ZonedDateTime object. In the next example, we use the ZoneOffset.UTC since Date object does not store zone information:

What is Mongo repository in Spring Boot?

MongoRepository — MongoRepository is used for basic queries that involve all or many fields of the document. Examples include data creation, viewing documents, and more. Spring Boot MongoDB configuration using both approaches needs only a few lines of code. Spring is an application framework for Java web applications.

How to use Spring Boot with MongoDB CRUD?

A common Spring Boot with MongoDB CRUD example could be a grocery shopping list of a user. A user may want to The CRUD operations are done using MongoRepository and MongoTemplate. We can use both by adding the spring-boot-starter-data-mongodb dependency in pom.xml.


1 Answers

Persisting date time types with time zones was never supported by Spring Data MongoDB, as stated by Oliver Drotbohm himself in DATAMONGO-2106.

These are the known workarounds:

  1. Use a date time type without a time zone, e.g. java.time.Instant. (It is generally advisable to only use UTC in the backend, but I had to extend an existing code base which was following a different approach.)
  2. Write a custom converter and register it by extending AbstractMongoConfiguration. See the branch converter in my test repository for a running example.

    @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. Write a custom codec. At least in theory. My codec test branch is unable to unmarshal the data when using Spring Boot 2.0.5 while working fine with 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;
        }
    }
    
like image 152
tdkBacke Avatar answered Oct 18 '22 19:10

tdkBacke