database - No converter found capable of converting from type [java.util.Date] to type [java.time.ZonedDateTime] - Stack Overflo

admin2025-04-21  2

I have the following MongoCfg Class

@Configuration
public class MongoCfg {

    @Bean
    public MongoCustomConversions customConversions() {
        return new MongoCustomConversions(Arrays.asList(
                new DateToZonedDateTimeConverter(),
                new ZonedDateTimeToDateConverter(),
                new BigDecimalDecimal128Converter(),
                new Decimal128BigDecimalConverter()
        ));
    }

    static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return source == null ? null : ofInstant(source.toInstant(), systemDefault());
        }
    }

    static class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }


    @WritingConverter
    static class BigDecimalDecimal128Converter implements Converter<BigDecimal, Decimal128> {

        @Override
        public Decimal128 convert(@NonNull BigDecimal source) {
            return new Decimal128(source);
        }
    }

    @ReadingConverter
    static class Decimal128BigDecimalConverter implements Converter<Decimal128, BigDecimal> {

        @Override
        public BigDecimal convert(@NonNull Decimal128 source) {
            return source.bigDecimalValue();
        }

    }


    @Primary
    @Bean(name = "alertDBProperties")
    @ConfigurationProperties(prefix = "spring.mongodb.alert")
    public MongoProperties getAlertProps() throws Exception {
        return new MongoProperties();
    }

    @Bean(name = "scanDBProperties")
    @ConfigurationProperties(prefix = "spring.mongodb.scan")
    public MongoProperties getScanProps() throws Exception {
        return new MongoProperties();
    }

    @Primary
    @Bean(name = "alertMongoTemplate")
    public MongoTemplate alertMongoTemplate() throws Exception {
        return new MongoTemplate(alertMongoDatabaseFactory(getAlertProps()));
    }

    @Bean(name ="scanMongoTemplate")
    public MongoTemplate scanMongoTemplate() throws Exception {
        return new MongoTemplate(scanMongoDatabaseFactory(getScanProps()));
    }

    @Primary
    @Bean
    public MongoDatabaseFactory alertMongoDatabaseFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoClientDatabaseFactory(
                mongo.getUri()
        );
    }

    @Bean
    public MongoDatabaseFactory scanMongoDatabaseFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoClientDatabaseFactory(
                mongo.getUri()
        );
    }
}

As you can see i handle 2 database connections, the problem is when i try to retrieve data i get this error:

No converter found capable of converting from type [java.util.Date] to type [java.time.ZonedDateTime]

I don't understand why this happens, i already declare DateToZonedDateTimeConverter and ZonedDateTimeToDateConverter

I have the following MongoCfg Class

@Configuration
public class MongoCfg {

    @Bean
    public MongoCustomConversions customConversions() {
        return new MongoCustomConversions(Arrays.asList(
                new DateToZonedDateTimeConverter(),
                new ZonedDateTimeToDateConverter(),
                new BigDecimalDecimal128Converter(),
                new Decimal128BigDecimalConverter()
        ));
    }

    static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return source == null ? null : ofInstant(source.toInstant(), systemDefault());
        }
    }

    static class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }


    @WritingConverter
    static class BigDecimalDecimal128Converter implements Converter<BigDecimal, Decimal128> {

        @Override
        public Decimal128 convert(@NonNull BigDecimal source) {
            return new Decimal128(source);
        }
    }

    @ReadingConverter
    static class Decimal128BigDecimalConverter implements Converter<Decimal128, BigDecimal> {

        @Override
        public BigDecimal convert(@NonNull Decimal128 source) {
            return source.bigDecimalValue();
        }

    }


    @Primary
    @Bean(name = "alertDBProperties")
    @ConfigurationProperties(prefix = "spring.mongodb.alert")
    public MongoProperties getAlertProps() throws Exception {
        return new MongoProperties();
    }

    @Bean(name = "scanDBProperties")
    @ConfigurationProperties(prefix = "spring.mongodb.scan")
    public MongoProperties getScanProps() throws Exception {
        return new MongoProperties();
    }

    @Primary
    @Bean(name = "alertMongoTemplate")
    public MongoTemplate alertMongoTemplate() throws Exception {
        return new MongoTemplate(alertMongoDatabaseFactory(getAlertProps()));
    }

    @Bean(name ="scanMongoTemplate")
    public MongoTemplate scanMongoTemplate() throws Exception {
        return new MongoTemplate(scanMongoDatabaseFactory(getScanProps()));
    }

    @Primary
    @Bean
    public MongoDatabaseFactory alertMongoDatabaseFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoClientDatabaseFactory(
                mongo.getUri()
        );
    }

    @Bean
    public MongoDatabaseFactory scanMongoDatabaseFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoClientDatabaseFactory(
                mongo.getUri()
        );
    }
}

As you can see i handle 2 database connections, the problem is when i try to retrieve data i get this error:

No converter found capable of converting from type [java.util.Date] to type [java.time.ZonedDateTime]

I don't understand why this happens, i already declare DateToZonedDateTimeConverter and ZonedDateTimeToDateConverter

Share Improve this question edited Jan 23 at 14:20 Bryan Saltos asked Jan 22 at 20:30 Bryan SaltosBryan Saltos 832 silver badges9 bronze badges 2
  • 2 have u annotated this class MongoCfg with @Configuration annotation ? – Ryuzaki L Commented Jan 23 at 2:49
  • yes, i did added that annotation, i just didtn notice i didnt copy the annotation – Bryan Saltos Commented Jan 23 at 14:20
Add a comment  | 

1 Answer 1

Reset to default 0

Finally can solve the error.

The first mistake on my side was not putting the @WritingConverter and @ReadingConverter annotation in the ZonedDateTimeToDateConverter and DateToZonedDateTimeConverter classes respectively.

The second error is that the MongoTemplate instances were not initialized with the respective mongoCustomConversions, thus the methods change as shown below:

    @Primary
    @Bean(name = "alertMongoTemplate")
    public MongoTemplate alertMongoTemplate() throws Exception {
        return new MongoTemplate(alertMongoDatabaseFactory(getAlertProps()));
    }


To this one here :

    @Primary
    @Bean(name = "alertMongoTemplate")
    public MongoTemplate alertMongoTemplate() throws Exception {
        MongoDatabaseFactory factory = alertMongoDatabaseFactory(getAlertProps());
        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(factory),
                new MongoMappingContext()
        );
        converter.setCustomConversions(customConversions());
        converter.afterPropertiesSet();
        return new MongoTemplate(factory, converter); 
    }
    

I'm not sure if it's the most efficient option but so far this has worked.

As an additional note, after making the change it gave me this error:

Unable to make private java.time.LocalDateTime(java.time.LocalDate,java.time.LocalTime) accessible: module java.base does not "opens java.time" to unnamed module @222545dc

Which was solved by adding this parameter to the jvm:

--add-opens java.base/java.time=ALL-UNNAMED

I hope this can help someone else with the same problem.

转载请注明原文地址:http://anycun.com/QandA/1745229437a90512.html