Throwing an error when Spring boot application tries to connect with mongodb atlas cluster

sandeep-prabhakula

Spring boot application throwing exception while connecting to mongodb atlas

I'm trying to connect spring boot application with a mongodb cluster and I'm facing the following issue while starting the spring boot application.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:659) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:647) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.9.jar:6.0.9]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:733) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305) ~[spring-boot-3.1.0.jar:3.1.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294) ~[spring-boot-3.1.0.jar:3.1.0]
    at com.sandeepprabhakula.blog.BlogApplication.main(BlogApplication.java:10) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-6.0.9.jar:6.0.9]
    ... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Database name must not be empty
    at org.springframework.util.Assert.hasText(Assert.java:294) ~[spring-core-6.0.9.jar:6.0.9]
    at org.springframework.data.mongodb.core.MongoDatabaseFactorySupport.<init>(MongoDatabaseFactorySupport.java:68) ~[spring-data-mongodb-4.1.0.jar:4.1.0]
    at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:75) ~[spring-data-mongodb-4.1.0.jar:4.1.0]
    at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:64) ~[spring-data-mongodb-4.1.0.jar:4.1.0]
    at org.springframework.boot.autoconfigure.data.mongo.MongoDatabaseFactoryConfiguration.mongoDatabaseFactory(MongoDatabaseFactoryConfiguration.java:47) ~[spring-boot-autoconfigure-3.1.0.jar:3.1.0]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.9.jar:6.0.9]
    ... 20 common frames omitted


Process finished with exit code 1

this is the application .properties file

spring.data.mongodb.uri=mongodb+srv://sandeepsandyo9o7o1:[email protected]/?retryWrites=true&w=majority
    
spring.data.mongodb.database=blog
Ilja S.

I had the same issue. Add this for workaround:

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

@Autowired
Environment env;

@Override
protected String getDatabaseName() {
    return env.getProperty("spring.data.mongodb.database", "test");
}

@Override
public MongoClient mongoClient() {
    String uri = env.getProperty("spring.data.mongodb.uri", "mongodb://localhost/test");
    ConnectionString connectionString = new ConnectionString(uri);
    MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
        .applyConnectionString(connectionString)
        .build();
    
    return MongoClients.create(mongoClientSettings);
}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can't connect MongoDB Atlas cluster to a Spring boot app

Error when trying to connect Spring boot application with mongoDB

Spring Boot Application Throwing Startup Failed Error When Trying to Run

Connect to MongoDB atlas cluster with mongoengine

Error : "Could not connect to any servers in your MongoDB Atlas cluster"

Connect laravel jenssegers to mongodb atlas cluster

Cannot connect to MongoDB Atlas Cluster: DNSHostNotFound

How to connect NodeJs to an Atlas mongodb Cluster

My command docker-compose up gives me an error when tries to run my spring-boot application

Mongoose.connect not throwing any error, when Mongodb is not running

Spring boot application error with connect PostgreSQL database

MongoDB Atlas Cluster Connection Problem with Authentication Error

Spring boot tries to connect to mongo when adding mongo-java-driver maven dependency

Node.js app cannot connect to MongoDB Atlas cluster

When I run my spring application, it is showing throwing an error

How to connect to specific local MongoDB instance in Spring Boot Dockerised application?

Connect To Docker Compose MongoDb Via Spring boot application

Javafx Spring Boot - Error When running Application

Spring boot with spring data for mongodb : Error on loading application

Spring Boot Starter-Web tries to connect to Mongo at startup

Not able to connect to mongoDB atlas

Enforce Spring Boot Application to Fail When Cannot Connect to Kafka

Node/express application failing to connect to Mongodb Atlas using mongoose

How to connect to mongoDB Atlas form a deployed application in google cloud?

How to connect to mongoDB Atlas from a deployed application in google cloud?

Throwing error when a particular field is missing or blank when using object mapper in spring boot

MongoDB: IllegalStateException: The pool is closed error message in Spring Boot application

Spring Application throwing error and WAR not building

Spring Application cannot connect to Kafka when it's run inside of the same cluster, but works when it runs from outside cluster