Spring Boot Webflux RDBMS Sample App

Overview

R2DBC Spring Boot Webflux Sample Project

  • generate schema (DDL script)
  • h2-console (Web based database management console)

WebFlux and r2dbc is not support schema generation and h2-console which are built in features of the Spring Boot MVC.

JPA schema generation gradle plugin and h2-console autoconfiguration library solve the problem.

Configuration

Gradle Plugin

Add JPA to schema file generation plugin. The spring boot is not using JPA, only gradle plugin and h2-console using jdbc connections and drivers. Application runtime is only using r2dbc driver and connections.

This plugin is using hibernate generate schema tool. You can use JPA annotations (javax.persistence.) with Spring annotations (org.springframework.data.) annotations at the same time without any issue HBM2DDL

build.gradle

plugins {
    // Other plugins...
    
    // JPA schema generation
    id "io.github.divinespear.jpa-schema-generate" version "0.4.0"
}

Dependencies

build.gradle

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-r2dbc')
    implementation('org.springframework.boot:spring-boot-starter-webflux')
    implementation('javax.persistence:javax.persistence-api')

    // PostgreSQL Database
    runtimeOnly('io.r2dbc:r2dbc-postgresql')
    runtimeOnly('org.postgresql:postgresql')
    // H2 Database
    runtimeOnly('io.r2dbc:r2dbc-h2')
    runtimeOnly('com.h2database:h2')

    generateSchemaLibs('org.hibernate:hibernate-core:5.5.7.Final')

    implementation ('me.yaman.can:spring-boot-webflux-h2-console:0.0.1')
}

Schema Generation

"outputDirectory" is depends on configuration. In this case postgres schema file is generated in the build directory. If you are using flyway or liquibase (You can use even with r2dbc application.) to manage migrations, you don't want to use the schema file without any modification. In that case, you can modify the file from the build directory.

build.gradle

generateSchema {
	vendor = 'hibernate'
	packageToScan = ["me.yaman.can.database.r2dbc.entity".toString()]
	scriptAction = 'create'
	format = true
	createOutputFileName = "V001__schema.sql"
	lineSeparator = 'CRLF'
	targets {
		postgres {
			outputDirectory = file("$buildDir/db/migration/postgres")
			databaseProductName = 'PostgreSQL Custom Dialect'
			properties = [
					'hibernate.hbm2ddl.delimiter': ';',
					'hibernate.dialect': 'org.hibernate.dialect.PostgreSQL10Dialect',
					'hibernate.physical_naming_strategy': 'org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy',
					'hibernate.implicit_naming_strategy': 'org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy'
			]
		}
		h2 {
			outputDirectory = file("src/main/resources/db/migration/h2")
			databaseProductName = 'H2 Custom Dialect'
			properties = [
					'hibernate.hbm2ddl.delimiter': ';',
					'hibernate.dialect': 'org.hibernate.dialect.H2Dialect',
					'hibernate.physical_naming_strategy': 'org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy',
					'hibernate.implicit_naming_strategy': 'org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy'
			]
		}
	}
}

Runtime Configuration

Flyway and Liquibase are also depends on JDBC driver. spring.sql.init.* properties can be used. Spring Boot R2dbc using basic sql scripts

application.yml

spring:
  sql:
    init:
      mode: always
      schemaLocations: classpath:/db/migration/h2/V001__schema.sql
      #dataLocations:

H2 Console Configuration

Spring autoconfiguration has to be enabled before (default is enabled). spring-boot-webflux-h2-console is autoconfiguration library and run as a separate web server.
spring.datasource part is optional for h2-console login. Even if it is optional, it is highly recommended. You don't have to sign in username, password and driver details; if it is available.

application.yml

spring:
  h2.console:
    enabled: true
    path: /h2-console
  datasource:
    url: 'jdbc:h2:mem:citydb'
    driverClassName: org.h2.Driver
    username: 'sa'
    password: ''

Demo

.\gradlew :app:generateSchema
.\gradlew :app:bootRun

First command generate h2 schema sql file and spring boot application is configured basic sql scripts by the generated sql file.

After bootRun task,

.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 47 ms. Found 1 R2DBC repository interfaces.
m.y.c.w.h.H2ConsoleAutoConfiguration     : H2 Page
m.y.c.w.h.H2ConsoleAutoConfiguration     : Connect to database jdbc:h2:mem:citydb
m.y.c.w.h.H2ConsoleAutoConfiguration     : spring.datasource.url is jdbc:h2:mem:citydb
m.y.c.w.h.H2ConsoleAutoConfiguration     : H2 Console JDBC datasource is configured
m.y.c.w.h.H2ConsoleAutoConfiguration     : H2 Console page /h2-console is configured for datasource jdbc:h2:mem:citydb
o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
m.y.c.w.h.H2ConsoleAutoConfiguration     : H2 Console started on port 57506
m.y.c.w.h.H2ConsoleAutoConfiguration     : H2 Web Console server running at http://192.168.1.106:57506 (others can connect)
m.y.can.database.r2dbc.ApplicationKt     : Started ApplicationKt in 3.048 seconds (JVM running for 3.49)

now you can manage database manually via h2-console http://localhost:8080/h2-console

You might also like...
My personal template for a Spring Boot REST reactive webapp

My personal spring boot kotlin reactive template Features Spring Security implementation with JWT access and refresh token MongoDB database Project Co

Generated with spring boot kotlin starter kit

Kotlin backend Generated with spring boot kotlin starter kit The idea is to: Get a microservice written in kotlin for managing users and roles. To be

The Okila server project uses the Spring boot framework and uses the Kotlin language

Okila-Server The Okila server project uses the Spring boot framework and uses the Kotlin language Format Response //The response successfully format

An application that simulate the Swedish Transport Agency, implemented with Spring Boot, Kotlin and GraphQL

graphql-kotlin-spring-server An application that simulate the Swedish Transport Agency, implemented with Spring Boot, Kotlin and GraphQL Running the s

API for a library using Kotlin, Spring-boot and covered by test

Library API This API is to create Books and be able to borrow from them I'm using: Spring Boot version 2.5.6 Kotlin 1.5.31 Java 11 I'm implementing us

Push notification application using One Signal + Spring Boot + Kotlin

Spring Boot push notification system using One Signal This project was created to demonstrate a simple application built with Kotlin + Spring Boot inc

Kotlin Based Spring Boot ETL project
Kotlin Based Spring Boot ETL project

Adverity ETL REST API (AERA) A simple ETL Spring Boot application with H2. This API provides the following basics functionalities: Endpoint that retur

Solution for task 5 on Spring Boot https://t.me/try2py/197

IouRestService (Spring Boot Application) Решение задачи 5 https://t.me/try2py/197 Условие задачи: Четверо соседей по комнате имеют привычку занимать д

GraphQL for Java with Spring Boot made easy.

GraphQL for Java with Spring Boot made easy.

Owner
Can Yaman
Can Yaman
Event-driven application uses React, reactive Spring Boot WebFlux, R2DBC, MySQL and Liquibase

Product delivery Event-driven application uses React, reactive Spring Boot WebFlux, R2DBC, MySQL and Liquibase Status: IN PROGRESS if [[ "" != `docker

Maksim Kostromin 2 Aug 17, 2022
Reactive setup with Spring WebFlux , Kotlin, Postgres and Spring Data R2DBC

Reactive Spring with Kotlin and Pg Spring WebFlux with Netty instead of Spring Web with Tomcat Mono and Flux in all layers (controller, service, repo)

Bimal Raj Gyawali 7 Dec 9, 2022
This repository demonstrates Spring GraphQL + RSocket + WebFlux + R2DBC + H2

Reactive GraphQL with Spring This repository demonstrates Spring GraphQL + RSocket + WebFlux + R2DBC + H2 O__ +-----------+

Maksim Kostromin 1 Nov 27, 2021
Spring-with-maven - Spring Boot App with Postgresql and maven

Spring Boot Api Aplikasi ini dibuat menggunakan bahasa kotlin dan untuk database

Aldi Aulia Rosyad 1 Jan 12, 2022
A sample skeleton backend app built using Spring Boot kotlin, Expedia Kotlin Graphql, Reactive Web that can be deployed to Google App Engine Flexible environmennt

spring-kotlin-gql-gae This is a sample skeleton of a backend app that was built using: Spring Boot(Kotlin) Reactive Web Sprinng Data R2DBC with MYSQL

Dario Mungoi 7 Sep 17, 2022
Spring-graphql-getting-started - Spring for GraphQL provides support for Spring applications built on GraphQL Java

Getting Started with GraphQL and Spring Boot Spring for GraphQL provides support

Shinya 0 Feb 2, 2022
Sample Social Login Project of Spring Boot and Kotlin

Sample-Spring-Boot-Social-Kotlin Sample Social Login Project of Spring Boot and Kotlin dependencies dependencies { implementation("org.springframewor

Seokhyun 2 Oct 11, 2021
Cloud Bowl Sample - Kotlin Spring Boot

Cloud Bowl Sample - Kotlin Spring Boot To make changes, edit the src/main/kotlin/hello/KotlinApplication.kt file. Run Locally: ./mvnw spring-boot:run

Lukasz Domzalski 0 Dec 13, 2021
Spring Boot Example with Kotlin and Domain Driven Design

Spring Boot Kotlin Domain Driven Design Example Just an example project where ddd is implemented with some other great patterns and architecture from

Fernando Guevara Sanchez 16 Jun 29, 2022
This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping

Kotlin spring-boot nested config props This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping @Constru

Maksim Kostromin 1 Oct 11, 2021