Monday 25 March 2019

org.bson.codecs.configuration.CodecConfigurationException: Unable to get value for property

Problem : 

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.time.LocalDate.

org.bson.codecs.configuration.CodecConfigurationException: Unable to get value for property


Solution :

Try creating your bean by this way to help POJO classes.


 @Bean
    public MongoDatabase mongoDatabase() {
        try {
            CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(),
                    fromProviders(PojoCodecProvider.builder().automatic(true).build()));
     
            String connectionString = "mongodb+srv://" + username + ":" + password + "@" + host + "/" + database + "";
            log.info("Connection String " + connectionString);
            MongoClientURI uri = new MongoClientURI(connectionString);
            MongoClient mongoClient = new MongoClient(uri);

            MongoDatabase database = mongoClient.getDatabase("mydb").withCodecRegistry(pojoCodecRegistry);
            return database;
        } catch (Exception e) {
            log.error("Error in Connection " + e);
            return null;
        }
    }



No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...