Solving the error Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified

This tutorial will help you to solve the error Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified in your Spring Boot applications.

In a nutshell, this error happens if you have configured in your pom.xml file the spring-boot-starter-data-jpa dependency and no datasource url is configured in application.properties

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

That will cause the following error:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class

Let’s see how to fix the issue.

Remove the JPA starter if not needed

If you added by mistake the JPA starter in your pom.xml or if you changed your persistence storage from RDBMS to MongoDB (as an example), just remove the jpa starter to fix the issue:

<!-- Remove this 
<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>

Include the database configuration in application.properties

The second option, if you are planning to use an external DB like MySQL, is to make sure your application.properties contains the required configuration and your pom.xml file has the JDBC Driver for your DB:

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

And there is the pom.xml file:

<dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 
<dependency> 
   <groupId>mysql</groupId> 
   <artifactId>mysql-connector-java</artifactId> 
   <scope>runtime</scope> 
</dependency>

Use an embedded Database

Finally, it’s worth mentioning that when using an Embedded Database, it is not required to specify connection settings in application.properties as you can have some default values for it. Make sure you have the dependency for JDBC in-memeory databases in your pom.xml. Example for H2 Database:

<dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 
<dependency> 
   <groupId>com.h2database</groupId> 
   <artifactId>h2</artifactId> 
   <scope>runtime</scope> 
</dependency>