-1

I have an Java Spring Boot Rest API application that works fine.

But when I add this dependency to pom.xml:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>

I start getting the following error:

2023-03-31 18:11:34.768[0;39m [32m INFO[0;39m [35m1612[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 2437 ms
[2m2023-03-31 18:11:34.872[0;39m [33m WARN[0;39m [35m1612[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
[2m2023-03-31 18:11:34.875[0;39m [32m INFO[0;39m [35m1612[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.a.c.c.StandardService                 [0;39m [2m:[0;39m Stopping service [Tomcat]
[2m2023-03-31 18:11:34.896[0;39m [32m INFO[0;39m [35m1612[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConditionEvaluationReportLoggingListener[0;39m [2m:[0;39m 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2023-03-31 18:11:34.924[0;39m [31mERROR[0;39m [35m1612[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter  [0;39m [2m:[0;39m 

***************************
APPLICATION FAILED TO START
***************************

Description:

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

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).

I know there are other posts here in StackOverflow about this error.

But couldn't find one with a solution for this specific cause I'm reporting here.

Thanks.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mycomp.myappapi</groupId>
    <artifactId>myappapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>myappapi</name>
    <description>My app</description>
    <properties>
        <java.version>11</java.version>
        <org.mapstruct.version>1.5.2.Final</org.mapstruct.version>
    </properties>
    <dependencies>
        <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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>de.huxhorn.sulky</groupId>
            <artifactId>de.huxhorn.sulky.ulid</artifactId>
            <version>8.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--<version>3.8.1</version>-->
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>11</source> <!-- depending on your project -->
                    <target>11</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
server.servlet.context-path=/xxx
spring.jpa.generate-ddl=true

#hikari
spring.datasource.hikari.connection-timeout = 20000 
spring.datasource.hikari.minimum-idle= 10
spring.datasource.hikari.maximum-pool-size= 40
spring.datasource.hikari.idle-timeout=10000 
spring.datasource.hikari.auto-commit =true

server.error.include-stacktrace=never

jkfe
  • 549
  • 7
  • 29
  • the error message posted and the dependency from the pom.xml does not match each other. You are getting the error because you have spring-data or spring-jpa or some other database related dependency in the pom, and when it starts up it is looking for a database to to connect to, and you havnt configured where the database is (url). Please post your FULL pom.xml – Toerktumlare Mar 31 '23 at 23:17
  • @Toerktumlare Added pom.xml and application.properties to the question. I don't think the database configuration is the problem because before adding the mentioned dependency, the api could successfully connect to the db and execute all CRUD operations. The error really started to happen just when spring-security-web was added to pom.xml. That is really the only single difference from the when everything was working fine including db connection and CRUD operations. – jkfe Apr 01 '23 at 01:34
  • Why are you not using the spring security starter dependency? – Toerktumlare Apr 01 '23 at 08:11
  • @Toerktumlare Should I? As I understand, for what I need to do, the only spring security dependencies I should need should be spring-security-config and spring-security-web. I posted an answer for my own question. See below. That happens when I have these two dependencies I mentioned in pom.xml. – jkfe Apr 01 '23 at 10:52
  • since you are not using the recommended way of including things, its impossible to tell, this most likely has to do with versions or spring autoconfiguration not loading properly when you do unorthodox things see this answer https://stackoverflow.com/a/66193743/1840146 if you dont go the recommended route, you can expect undefined behavior. – Toerktumlare Apr 01 '23 at 11:24
  • when you say I'm not using the recommended way, what would be the recommended way? Thought this was the way to do it. But if not, would be glad to change to what it should be. Thanks. – jkfe Apr 03 '23 at 12:57

1 Answers1

0

I found a solution for the question I posted although I'm not yet satisfied with it cause it has a strange behaviour.

The solution is to change:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

to:

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

When that is done, the application runs fine.

The strange part is that if I right click the project name in Spring Tool Suite (Eclipse) > Maven > Update Project, to update maven dependencies, the solution stops working. And no changes at all were made either on the code or in pom.xml. Then I change it back to:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

and starts working again!

And if I do another maven update, it stops working again and need to change once more to the previous version of the property.

Makes no sense. It works just if I don't do a maven update. Whenever I do a maven update I need to change to the other version of the property.

So, sometimes one version of the property is the one that works, sometimes is the other.

Really strange.

jkfe
  • 549
  • 7
  • 29