Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified
Solution 1
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.
Solution 2
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
Solution 3
Add the line below in application.properties file under resource folder and restart your application.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Solution 4
Seems there is missing MongoDB driver. Include the following dependency to pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Solution 5
I encountered this error simply because I misspelled the spring.datasource.url
value in the application.properties file and I was using postgresql:
Problem was:
jdbc:postgres://localhost:<port-number>/<database-name>
Fixed to:
jdbc:postgresql://localhost:<port-number>/<database-name>
NOTE: the difference is postgres
& postgresql
, the two are 2 different things.
Further causes and solutions may be found here
Related videos on Youtube
Subash J
Experience in creating eclipse plug-ins, Rich client applications (RCP - e3 and e4) , JavaFX Love to play games like Counter Strike and NFS
Updated on March 29, 2022Comments
-
Subash J about 2 years
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
-
Subash J about 6 yearsThanks for the answer. I already have this dependency in my pom.xml file. Still failing.
-
Subash J about 6 yearsYes that was my problem. I have removed the JPA dependency and it works fine now. Thank you.
-
tero17 about 6 yearsit failed for me too with spring-boot-starter-data-mongodb & spring-batch-starter
-
Roberto Lo Giacco almost 6 yearsPlease note also
spring-boot-starter-batch
introduces a dependency to jdbc which activates this same error -
WesternGun almost 6 yearsYes that solved my problem. In my case was
org.postgresql:postgresql
anddata-jpa
. Just excludedata-jpa
and all is fine. -
whatthefish almost 6 yearsWould it have created an error if I had
data-jpa
andmysql-connector-java
in my pom.xml? -
Lucky almost 6 yearsI hided the mongo db dependency but still had the same problem.
-
Bhabadyuti almost 6 yearsI guess you should not remove the mongo dependancy instead remove the data-jpa dependancy.
-
fuat over 5 yearsI have got this problem even i didn't use jpa and mongodb dependencies together in my pom.xml... So i am getting it cuz
spring-boot-starter-batch
dependency. Can you please explain why this setting is work ? -
Jatinder about 4 yearsThanks for answering it helps a lot I too resolved the issue by your given answer.
-
Alan over 3 yearsFor mongodb: use mongo dependency
-
Adir D over 3 yearsIt worked, but then i got error when sending request from client side:
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
-
Taimoor Ahmad over 3 years@AdirD That has nothing to do with the issue at hand, you need to set your server to accept CORS requests
-
Taimoor Ahmad over 3 yearsI had this issue when I was switching my Spring project from Postgres to MongoDB. If you are using IntelliJ, please remember to do a MAVEN SYNC after editing your
pom.xml
! -
ysldl about 2 yearsI also faced with the problem, after changed packaging from war to jar. I have an application.properties file which has a profile value and then application-profileName.properties file is used to get db connection parameters. Interestingly, when works after changed back to war.