In Creating a Spring Boot application with a flyway H2 embedded database I am having trouble getting the SQL file to run - spring-boot

I have my SQL file titled V2__create_shipwrech.sql. The above file is located in the migration folder of the following path das-boot/src/main/resources/db/migration.
Here's the SQL content:
CREATE TABLE SHIPWRECK(
ID INT AUTO_INCREMENT,
NAME VARCHAR(255),
DESCRIPTION VARCHAR(2000),
CONDITION VARCHAR(255),
DEPTH INT,
LATITUDE DOUBLE,
LONGITUDE DOUBLE,
YEAR_DISCOVERED INT
);
Here's my application.properties file contents:
logging.level.org.springframework.web=DEBUG
server.port=8080
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.max-wait=10000
spring.datasource.min-evictable-idle-time-millis=1000
spring.datasource.min-idle=8
spring.datasource.time-between-eviction-runs-millis=1
flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=false;
Here's my pom file contents:
<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>
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
This is my first time working with Spring Boot, Flyway, and the h2 embedded database, so please for give me if I sound like a newbie at this, I am.
Here's my console output lines I think stand out to me:
- Database: jdbc:h2:file:~/dasboot (H2 1.4)
- Validated 1 migration (execution time 00:00.039s)
- Current version of schema "PUBLIC": 2
- Schema "PUBLIC" is up to date. No migration necessary.
- Building JPA container EntityManagerFactory for persistence unit 'default'
- Processing PersistenceUnitInfo [
- name: default
- ...]
- Hibernate Core {4.3.11.Final}
- hibernate.properties not found
- HHH000021: Bytecode provider name : javassist
- Hibernate Commons Annotations {4.0.5.Final}
- Unrecognized value for "hibernate.hbm2ddl.auto": false;
- Looking for #ControllerAdvice:
Then there were a ton of debug statements saying bean that were rejected after that, which I don't have enough room to paste.
This was all using java version 1.8
Any help would be greatly appreciated and what I learn from you will be used to teach others as I progress.
Thanks in advance,
Joe

Modify the property spring.jpa.hibernate.ddl-auto=false to
spring.jpa.hibernate.ddl-auto=none

Related

Deploy Spring Boot application on AWS (Elastic Beanstalk) that uses RDS - Application does not start

I have a SpringBoot application connected to a MySQL database. This has already been installed on AWS and entered into Spring's properties file. The connection works and I can also connect to it with my workbench. But my problem is that the application does not run on AWS. I changed the SERVER_PORT to 5000 (also in the properties) but I can't get the APP to run
The AWS log file:
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 https://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.6.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.backend</groupId>
<artifactId>SYFALL</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SYFALL</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties file:
server.port = 5000
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.datasource.url = jdbc:mysql://AWS_DATABASE:3306/syfall
spring.datasource.username = xxx
spring.datasource.password = xxx
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
The application was built as a JAR file.
As stated in the comments, I have my sample Spring BOOT app that uses MySQL RDS data and queries and displays data perfectly.
I am currently in the process of deploying to Elastic Beanstalk. I will update this thread with notes once done.
UPDATE
I successfully deployed this Spring BOOT app to Elastic Beanstalk and got the RDS Connection working. I packaged up the Spring BOOT app into a JAR by using mvn package. Then I deployed the JAR onto Elastic Beanstalk.
Notes:
Be sure to specify your environment variables correctly in the Elastic Beanstalk environment (as opposed to a Spring config file) -- as shown here:
The AWS Creds are needed if your app uses an AWS Service Client. I use SES in this app so I need them
Now here is the important part. You need to set the Inbound rules so that a connection from the app deployed on EB to RDS can be made. I simply used this for testing....
To take this further, you can of course use the IP Address of your app on Beanstalk in your Inbound Rule. To do so, get the IP of your Elastic Beanstalk app. Then use that IP in your inbound rule.
Make sure that the Security Group your RDS uses is also the same Security Group your EB app uses. Both RDS and EB has to be in same region.

Spring Boot Application displays no error, but tomcat not initialized and application.properties is unused

I created a new project using Spring Initializr, and opened it with Intellij IDEA version 2020.3.2 ide. After maven installed all dependencies in the pom.xml file, I ran the main method in the DemoApplication class. I didn't add anything new or different than the default project created by the Spring Initializr.
Two Problems occured:
1- Console outputted only these 3 logs, and Tomcat wasn't initialized
2021-02-03 23:48:45.073 INFO 15872 --- [ main] com.example.DemoApplication: Starting DemoApplication using Java 1.8.0_281 on DESKTOP-M with PID 15872 (D:\demo\target\classes started by M in D:\demo)
2021-02-03 23:48:45.078 INFO 15872 --- [ main] com.example.DemoApplication : No active profile set,falling back to default profiles: default
2021-02-03 23:48:46.355 INFO 15872 --- [ main] com.example.DemoApplication: Started DemoApplication in 2.062 seconds (JVM running for 2.87)
Process finished with exit code 0
2- Anything I write in the application.properties file turns grey (unused)
I watched a lot of tutorials, and did exactly as they do. Their console output always has more than 3 statements and is even colored, which mine isn't
My 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 https://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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Would appreciate it if you would help me figure out how to correctly run the project and have the server up and running.
Should I add anymore run Options?
External Libraries:
This issue is reproducible with a simple war initializer, when you run DemoApplication with a "standalone java" (IDE run) configuration.
The solution/correct usage is: To run (the project) in a "maven" spring-boot:run (IDE run) configuration.
Like:
It looks like you are using IntelliJ IDEA Community Edition which has no support for Spring Boot.
That is why the properties are not highlighted and the output is not colored.
IntelliJ IDEA Ultimate will use Spring Boot Run/Debug configuration type automatically:
As a workaround you can run your code via Maven spring-boot:run.
I saw the same behavior when I setup a project with pox.xml you provided.
The "Include dependencies with "Provided" fixed it for me. However I had to reload the Maven dependencies like this a few times: pom.xml (right click) -> Maven -> Reload Project.
I would still suggest that you remove <scope>provided</scope> in spring-boot-starter-tomcat.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- Comment this out -->
<!-- <scope>provided</scope> -->
</dependency>
Here is a good answer - Java Spring Boot - spring-boot-starter-tomcat dependency doesn't work with scope provided when running locally
If you still need this project to run in an external Tomcat container and thus want to mark the spring-boot-spring-boot-starter dependency as "provided", you can specify a separate profile for that configuration - https://stackoverflow.com/a/42302247

Problem extending Spring WebFlux application with reactive DB client: conflicting Maven dependencies?

I have a small working Spring WebFlux app made basing on this code:
https://github.com/chang-chao/spring-webflux-reactive-jdbc-sample
As far as I've understood this is some kind of mix between purely reactive programming and usual blocking relational databases.
Now I have a task to add reactive DB client to my app. I stared with this guide:
https://spring.io/guides/gs/accessing-data-r2dbc/
But as soon as I added following dependencies to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>0.8.4.RELEASE</version>
<scope>runtime</scope>
</dependency>
my WORKING app failed to start saying it couldn't find autowired repository bean. This error disappeared as soon as I've removed two dependencies above.
Initial full pom.xml of WORKING app:
<?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 https://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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.freelance</groupId>
<artifactId>studentlist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-list</name>
<description>Spring WebFlux application for managing students</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.4.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I suppose some dependencies are conflicting with each other. Is there a way to actually use these dependencies with all my other dependencies so I'll be able to follow that guide?
I don't know if app's Java code is needed for this question, and if it is, which pieces. For now I'll just add application.properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:~/studentlist
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true
logging.level.org.springframework=warn
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
logging.level.org.hibernate.SQL=warn
logging.level.io.netty=warn
spring.datasource.maximum-pool-size=100
Adding either of these two dependencies, spring-boot-starter-data-r2dbc or r2dbc-h2, without the second one is enough to cause this error:
2020-10-20 15:31:26.008 WARN 11580 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.freelance.studentlist.repository.StudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-10-20 15:31:26.123 ERROR 11580 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentRepository in com.freelance.studentlist.service.StudentServiceImpl required a bean of type 'com.freelance.studentlist.repository.StudentRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.freelance.studentlist.repository.StudentRepository' in your configuration.
Process finished with exit code 1
I removed JPA dependency and I'm currently trying to make things work with just r2dbc.
I suppose my application.properties would no longer be valid. Could you please help me with changing it? I use this code fragment from the guide:
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
#Bean
public H2ConnectionFactory connectionFactory() {
return new H2ConnectionFactory(
H2ConnectionConfiguration.builder()
.url("jdbc:h2:~/studentlist;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4")
.username("sa")
.build());
}
}
I strongly suspect that such url is not valid for r2dbc. What would be valid substitution for jdbc:h2:~/studentlist URL in case of r2dbc H2 (not in-memory, just local DB)?
How should I change this block of code in application.properties ? URL in particular!
**spring.datasource.url=jdbc:h2:~/studentlist**
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
Please help if you know! Can't google an appropriate example for now...
The spring.datasource is for the traditional Jdbc data source, to configure an R2dbc connection, use spring.r2dbc prefix instead in Spring Boot application.properties.
Check my Spring R2dbc example, if you are new to Spring Data R2dbc, the docs in Readme.md.

How to run flyway migration for reactive r2dbc driver on sprintboot stratup

I am working on springboot webflux project with non-blocking database driver r2dbc,
But when Springboot application start Flyway does not run migrations.
Below are my spring-boot 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 https://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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>r2dbmigration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>r2dbmigration</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
My migration file is : V1.1__create_file_import_table.sql
with content of
DROP TABLE IF EXISTS file_import;
CREATE TABLE file_import
(
id BIGINT GENERATED ALWAYS AS IDENTITY,
file_key CHARACTER VARYING(255) NOT NULL,
created_at TIMESTAMP without time zone NOT NULL,
created_by BIGINT NOT NULL,
PRIMARY KEY (id)
);
application.properties
spring.r2dbc.url= r2dbc:postgresql://localhost:5432/import
spring.r2dbc.username=postgres
spring.r2dbc.password=password
My application starts smoothly but there is no migration run.
Can someone please help me ?
Github URL
Thanks
The following Java implementation is based on #Sim's Kotlin example.
Example 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 https://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.3.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>flyway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>flyway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
Example application.yml
---
spring:
r2dbc:
url: r2dbc:pool:mysql://localhost:3306/defaultdb
username: <user>
password: <pass>
flyway:
url: jdbc:mysql://localhost:3306/defaultdb
user: ${spring.r2dbc.username}
password: ${spring.r2dbc.password}
baseline-on-migrate: true
Add the following Spring Boot configuration - FlywayConfig.java
package com.example.flyway;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
// https://stackoverflow.com/a/61412233
#Configuration
public class FlywayConfig {
private final Environment env;
public FlywayConfig(final Environment env) {
this.env = env;
}
#Bean(initMethod = "migrate")
public Flyway flyway() {
return new Flyway(Flyway.configure()
.baselineOnMigrate(true)
.dataSource(
env.getRequiredProperty("spring.flyway.url"),
env.getRequiredProperty("spring.flyway.user"),
env.getRequiredProperty("spring.flyway.password"))
);
}
}
First execution:
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
: Finished Spring Data repository scanning in 7ms. Found 0 R2DBC repository interfaces.
: Flyway Community Edition 6.4.1 by Redgate
: Database: jdbc:mysql://localhost:3306/defaultdb (MySQL 5.5)
: Successfully validated 1 migration (execution time 00:00.006s)
: Creating Schema History table `defaultdb`.`flyway_schema_history` ...
: DB: Name 'flyway_schema_history_pk' ignored for PRIMARY key. (SQL State: 42000 - Error Code: 1280)
: Current version of schema `defaultdb`: << Empty Schema >>
: Migrating schema `defaultdb` to version 1.0.001 - Initialise database tables
: Successfully applied 1 migration to schema `default` (execution time 00:00.036s)
: Netty started on port(s): 8080
:Started Application in 1.829 seconds (JVM running for 2.343)
Second execution:
: No active profile set, falling back to default profiles: default
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
: Finished Spring Data repository scanning in 11ms. Found 0 R2DBC repository interfaces.
: Flyway Community Edition 6.4.1 by Redgate
: Database: jdbc:mysql://localhost:3306/defaultdb (MySQL 5.5)
: Successfully validated 1 migration (execution time 00:00.009s)
: Current version of schema `defaultdb`: 1.0.001
: Schema `defaultdb` is up to date. No migration necessary.
: Netty started on port(s): 8080
: Started Application in 1.273 seconds (JVM running for 1.695)
A bit late answer, but you can also set up a Spring bean manually to handle the migration.
Add flyway config to application.yml (or .properties):
spring:
flyway:
url: jdbc:postgresql://localhost:5432/<db-name>
user: <user>
password: <password>
This code is Kotlin, but it can easily be translated to Java:
import org.flywaydb.core.Flyway
import org.springframework.boot.autoconfigure.flyway.FlywayProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
#Configuration
class FlywayConfig(private val flywayProperties: FlywayProperties) {
#Bean(initMethod = "migrate")
fun flyway(): Flyway? {
return Flyway(Flyway.configure()
.baselineOnMigrate(true)
.dataSource(flywayProperties.url, flywayProperties.user, flywayProperties.password)
)
}
}
Solved this by using the combination of Flyway + JDBC, and R2DBC for the rest of DB interactions.
application.properties
my.database.url=postgresql://localhost:5432}/my_database
spring.r2dbc.url=r2dbc:${my.database.url}
spring.r2dbc.username=user
spring.r2dbc.password=pass
spring.flyway.locations=classpath:db/migration
spring.flyway.enabled=true
spring.flyway.validate-on-migrate=true
spring.flyway.user=${spring.r2dbc.username}
spring.flyway.password=${spring.r2dbc.password}
spring.flyway.url=jdbc:${my.database.url}
build.gradle
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
runtimeOnly 'org.flywaydb:flyway-core:7.9.1'
runtimeOnly 'org.postgresql:postgresql:42.2.20'
runtimeOnly 'io.r2dbc:r2dbc-postgresql'
runtimeOnly 'org.springframework.boot:spring-boot-starter-jdbc'
...
}
Location of flyway migration SQL scripts:
src/main/resources/db/migration/V1__create_my_table.sql
It seems like there is no "official" solution for this problem yet, but there is a temporary solution to solve that: "R2DBC migration tool".
#Nikita Konev provide us a good temporary solution for this problem. I have been using that and works well.
Please check:
R2DBC support - Flyway: https://github.com/flyway/flyway/issues/2502
R2DBC migration tool: https://github.com/nkonev/r2dbc-migrate
Example project (you can play around): https://github.com/nkonev/r2dbc-migrate-example
Thanks to Nikita Konev
This might be delayed response, but personally, I prefer to just add the spring starter.
This worked for me:
// build.gradle.kts
implementation("org.flywaydb:flyway-core:7.9.1")
runtimeOnly("org.springframework.boot:spring-boot-starter-jdbc")
runtimeOnly("org.postgresql:postgresql:42.2.20")
Then your application.yml file can look like:
# application.yml
spring:
r2dbc:
url: r2dbc:postgresql://user:pass#host/db
flyway:
enabled: true
validate-on-migrate: true
user: user
password: pass
url: jdbc:postgresql://host:5432/db
schemas: ["schema"]
As mentioned in the comments: R2DBC and Flyway are not compatible yet.
A workaround is to execute Flyway migration using the Maven Flyway plugin, note that you need to provide a JDBC-URL.
In your case, you can trigger the migration via
mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5432/import -Dflyway.user=postgres -Dflyway.password=password
or via
mvn flyway:migrate
if you add the plugin+configuration in your pom.xml
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.1.3</version>
<configuration>
<url>jdbc:postgresql://localhost:5432/import</url>
<user>postgres</user>
<password>password</password>
</configuration>
</plugin>
execute the migration via

Failed to load class "org.slf4j.impl.StaticLoggerBinder", Spring Boot

I'm using Spring Boot 1.5.8. SLF4J doesn't seem to be working for me. Here is what I get in the console at the beginning of a project run. The below error shows appears :
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
START : EDIT 1 :
My console doesn't log the below:
LOGGER.error("ERROR");
LOGGER.error("WARN");
LOGGER.error("INFO");
LOGGER.error("DEBUG");
LOGGER.error("TRACE");
All I get is those three lines of error(warning) as the first lines in my console.
END : EDIT 1 :
I looked up the internet and found multiple issues titled as this,tried the suggestions but that didn't resolve my issue. I came across this stackoverflow question where Konstantinos hints at the possibility of presence of multiple slf4j dependencies. Other solutions on the internet also suggested that; even the official site confirmed that to be the reason for such error.
Also checked my effective pom just to eliminate this possibility and found that my effective pom does contain all the 4 dependencies that he mentioned. The general suggestion is that the pom should contain only one of these four dependencies. But , since this was all handled by Spring boot, I'm a bit confused as to how do I go about resolving this, because I never exclusively included any slf4j dependency in my pom. So I'm unable to follow the suggestion.
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>
<groupId>com.codingethics.flightreservation</groupId>
<artifactId>flightreservation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>flightreservation</name>
<description>Flight Reservation Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties :
spring.datasource.url=jdbc:mysql://localhost:3306/reservation
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.show-sql=true
spring.mvc.view.prefix=/WEB-INF/jsps/
spring.mvc.view.suffix=.jsp
server.context-path=/flightreservation
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<myusername>
spring.mail.password=<mypwd>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
Why does this dependency-conflict(if at all that's the case here) happen in a Spring Boot project. Isn't Spring Boot meant to be looking after these beforehand.
Any sort of help/ guidance is highly appreciated.
START : EDIT for #Antot
I get NoClassDefFoundError.(followed steps as advised by you) :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
at com.codingethics.flightreservation.FlightreservationApplication.main(FlightreservationApplication.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
END : EDIT for #Antot
You might first want to check if something that isn't managed by Spring Boot is pulling in additional dependencies. You can do this with mvn dependency:tree to see what is pulling in what.
If that doesn't give a hint in the right direction it might be that one of your downloaded dependencies got borked. You might want to purge the local repository. You can do this with mvn dependency:purge-local-repository to remove all your dependencies from your local maven repository.
Then when doing mvn package (or another task that will compile and build the jar) will (re)download the jars and hopefully in a correct way without corruption.

Resources