Couldn't connect spring boot application with mongoDb atlas.

This is connection string I have recieved from Atlas UI : mongodb+srv://:@work.b83isih.mongodb.net/?retryWrites=true&w=majority&appName=work

Then i pasted this string in → spring.data.mongodb.uri = connection string
where i replaced my username and password. Other than this property , only 2 other properties are specified in my application.properties :

spring.data.mongodb.database = journaldb

spring.data.mongodb.auto-index-creation = true

On running it gives me a long list of errros mainly because it couldn’t establish connection with mongodb atlas. Here is how the errror starts :

2024-07-01T01:58:37.670+05:30  INFO 3172 --- [Project] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-07-01T01:58:37.682+05:30  INFO 3172 --- [Project] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-07-01T01:58:37.682+05:30  INFO 3172 --- [Project] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.25]
2024-07-01T01:58:37.746+05:30  INFO 3172 --- [Project] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-07-01T01:58:37.747+05:30  INFO 3172 --- [Project] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1216 ms
2024-07-01T01:58:52.978+05:30  WARN 3172 --- [Project] [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'journalEntryController': Unsatisfied dependency expressed through field 'journalEntryService': Error creating bean with name 'journalEntryService': Unsatisfied dependency expressed through field 'journalEntryRepository': Error creating bean with name 'journalEntryRepository' defined in com.springSecurity.Project.repository.JournalEntryRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
2024-07-01T01:58:52.982+05:30  INFO 3172 --- [Project] [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2024-07-01T01:58:52.992+05:30  INFO 3172 --- [Project] [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.

Hi @yuvraj_chandra and welcome to the community forum.

The above error message do not clearly indicate the issue that could be presemnt with the connection string.
I would recommend you to look for the following:

  1. Build your application using all the dependencies for the Spring Boot using mvn clean package or gradle build depending on the type of application.
  2. Check for the correct versions and the compatibility between the versions.
  3. Verify if you are able to make successful connection with Atlas outside the application. Example using Shell or Compass.
  4. Finally, would recommend you to enable DEBUG looging for the application using
    logging.level.org.springframework=DEBUG in the application.properties file.

Also, if you wish to follow a tutorial, the recommendation would be to check the code example provided on the article REST APIs with Java, Spring Boot, and MongoDB.

Please feel free to reach out in case of further questions.

Best Regards
Aasawari