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

142,655

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

Share:
142,655

Related videos on Youtube

Subash J
Author by

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, 2022

Comments

  • Subash J
    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
    Subash J about 6 years
    Thanks for the answer. I already have this dependency in my pom.xml file. Still failing.
  • Subash J
    Subash J about 6 years
    Yes that was my problem. I have removed the JPA dependency and it works fine now. Thank you.
  • tero17
    tero17 about 6 years
    it failed for me too with spring-boot-starter-data-mongodb & spring-batch-starter
  • Roberto Lo Giacco
    Roberto Lo Giacco almost 6 years
    Please note also spring-boot-starter-batch introduces a dependency to jdbc which activates this same error
  • WesternGun
    WesternGun almost 6 years
    Yes that solved my problem. In my case was org.postgresql:postgresql and data-jpa. Just exclude data-jpa and all is fine.
  • whatthefish
    whatthefish almost 6 years
    Would it have created an error if I had data-jpa and mysql-connector-java in my pom.xml?
  • Lucky
    Lucky almost 6 years
    I hided the mongo db dependency but still had the same problem.
  • Bhabadyuti
    Bhabadyuti almost 6 years
    I guess you should not remove the mongo dependancy instead remove the data-jpa dependancy.
  • fuat
    fuat over 5 years
    I 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
    Jatinder about 4 years
    Thanks for answering it helps a lot I too resolved the issue by your given answer.
  • Alan
    Alan over 3 years
    For mongodb: use mongo dependency
  • Adir D
    Adir D over 3 years
    It 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
    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
    Taimoor Ahmad over 3 years
    I 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
    ysldl about 2 years
    I 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.