Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified

I have created a basic spring boot application from SPRING INITIALIZR with the Web, MongoDB and JPA dependencies.

When I try to run the spring boot application I am getting the following exception:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-25 16:27:02.807 ERROR 16256 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :   *************************** APPLICATION FAILED TO START *************************** Description: Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured. Reason: Failed to determine a suitable driver class  Action:  Consider the following situation: If you want an embedded database like H2, HSQL or Derby, please add it in the Classpath. If you have database settings to be loaded from a particular profile you may need to activate it since no profiles were currently active. 

In application.properties file I am having the following configuration:

server.port=8081 spring.data.mongodb.database=TestDatabase spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 

Versions which I use: Spring : 5.0.4, MongoDB : 3.6, Spring Boot: 2.0

like image 274
Subash J Avatar asked Mar 25 '18 11:03

Subash J


People also ask

What is spring DataSource URL?

A DataSource is a factory for connections to the physical databases. It is an alternative to the DriverManager facility. A datasource uses a URL along with username/password credentials to establish the database connection. In Java, a datasource implements the javax.

How do you fix failed to determine a suitable driver class?

Error Message Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

How does Spring Boot define DataSource?

Spring Boot provides a very good support to create a DataSource for Database. We need not write any extra code to create a DataSource in Spring Boot. Just adding the dependencies and doing the configuration details is enough to create a DataSource and connect the Database.


2 Answers

Since you have added both mongodb and data-jpa dependencies in your pom.xml file, it was creating a dependency conflict like below

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> 

Try removing jpa dependency and run. It should work fine.

like image 160
Bhabadyuti Avatar answered Sep 30 '22 07:09

Bhabadyuti


Go to resources folder where the application.properties is present, update the below code in that.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 
like image 43
pushpendra yadav Avatar answered Sep 30 '22 06:09

pushpendra yadav