54

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
  • 2,028
  • 3
  • 13
  • 27

13 Answers13

117

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.

Bhabadyuti
  • 1,249
  • 1
  • 9
  • 13
63

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
  • 2
    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 ? – fuat Nov 20 '18 at 14:52
  • 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` – Adir Dayan Dec 29 '20 at 20:31
  • @AdirD That has nothing to do with the issue at hand, you need to set your server to accept CORS requests – Taimoor Ahmad Jan 03 '21 at 19:28
22

Add the line below in application.properties file under resource folder and restart your application.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
kiwifrog
  • 764
  • 3
  • 10
  • 23
bikram sahoo
  • 229
  • 2
  • 2
3

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
2

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>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
1

your dependency based on data is trying to find their respective entities which one has not been created, comments the dependencies based on data and runs the app again.

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

This error occurred when you are putting JPA dependencies in your spring-boot configuration file like in maven or gradle. The solution is: Spring-Boot Documentation

You have to specify the DB connection string and driver details in application.properties file. This will solve the issue. This might help to someone.

Community
  • 1
  • 1
Atul
  • 3,043
  • 27
  • 39
1

if anyone is here after facing this issue
when "project.jar" is generated
while every thing is fine when i the project is run in IDE / STS (spring tool suit).
here is a way out:

unnecessary spaces " " in the "application.yml" file can cause this.

server:
  port: 8085


spring:
 datasource:
  url: jdbc:mysql://localhost:3306/studentdb
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
 jpa:
   hibernate:
    ddl-auto: update
   show-sql: true
   database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
 application:
  name: STUDENT-SERVICE

instead of tweaking my "application.yml" file
i simply moved all my statements in "application.yml" file to
"application.properties" file and formatted the statements like required in ".properties".

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true

spring.application.name=student-service

server.port=8085

and voilà

sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
  • 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. – ysldl Apr 21 '22 at 22:37
0

Add your dependencies like mongodb,web,jpa. Delete/clear the remainings.

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
0

In gradle build i simply:

compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-devtools')

removed

**`compile('org.springframework.boot:spring-boot-starter-data-jpa')`**

and it worked for me.

ngPranav
  • 141
  • 1
  • 5
0

@Bhabadyuti Bal give us a good answer, in gradle you can use :

compile 'org.springframework.boot:spring-boot-starter-data-jpa' 
compile 'com.h2database:h2'

in test time :

testCompile 'org.reactivecommons.utils:object-mapper:0.1.0'
testCompile 'com.h2database:h2'
daniveloper
  • 9
  • 1
  • 4
-1

adding org.apache.derby dependency solved my issue.

<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
-1

I ran into this problem when I simply mistyped my jdbc url in application.properties. Hope this helps someone: before:

spring.datasource.url=jdbc://localhost:3306/test

after:

spring.datasource.url=jdbc:mysql://localhost:3306/test
Prashant Saraswat
  • 838
  • 1
  • 8
  • 20