2

I have this structure

project  (module.pom)
- connection                     (module.jar)
      - src/main/java
            -com.mycompany.connection
                -connectionBD.class
      - ...
      - pom.xml
- person                         (module.jar)
      - src/main/java
            - com.mycompany.person
                -personApplication.class
            - com.mycompany.person.controller
                -...
            - com.mycompany.person.model
                -...
      - src/main/resources
      - ...
      - pom.xml
pom.xml

my connectionBD.class class is this.

@Configuration
public class connectionBD {

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory(){
        return new SimpleMongoClientDatabaseFactory("mongo://localhost:27017/lydsam");
    }

    @Bean
    public MongoTemplate mongoTemplate() {
        return new MongoTemplate(mongoDatabaseFactory());
    }
}

and my PersonApplication.class class that is in another module.

@SpringBootApplication(scanBasePackages = { "com.mycompany.person", "com.mycompany.connection" })
public class PersonApplication {

    public static void main(String[] args) {
        try {
            SpringApplication.run(PersonApplication.class, args);
        } catch (Exception e) {
            System.out.println("Error: "+ e.getMessage());
        }
    }
}

When I run the project, I get an error saying that my connection to my database is wrong and I don't know what the problem is. Somebody could help me.

Error message

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/mycompany/connection/connectionBD.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [com/mycompany/connection/connectionBD.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDatabaseFactory]: Factory method 'mongoDatabaseFactory' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with either 'mongodb://' or 'mongodb+srv://
2
  • Could you please share the log? Commented Jun 28, 2020 at 23:20
  • 1
    Remove your connectionDB class/configuration and let Spring Boot do it. Just add the spring.data.mongodb.uri=<your-config-url> and Spring Boot will set things up for you. See docs.spring.io/spring-boot/docs/current/reference/html/… Commented Jun 29, 2020 at 5:59

1 Answer 1

1

Connection strings must start with either 'mongodb://' or 'mongodb+srv://

You need to edit. mongo -> mongodb SimpleMongoClientDatabaseFactory

@Configuration
public class connectionBD {

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory(){
        return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/lydsam");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

OR SimpleMongoClientDbFactory if you are using older version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.