How to enable H2 console with Reactive REST API in Spring Boot

Spring Boot H2 console enables reactive streaming Examples

Purpose: In this guide, I want to show how to enable the in-memory h2 console in Spring Boot with reactive mono.

1. Create reactive API Visit hereHow to create Reactive REST API in Spring Boot.

Next Step. Add in-memory database dependency.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Next Step: The default server port is 8080. Wish to change port 
 
server.port=8080


# Basic property
spring.application.name = reactive-basic
server.port=8080
spring.webflux.base-path=/api

# H2-database property
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#enabling the H2 console
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:C:/data/reactive
 
# Custom H2 Console URL
spring.h2.console.path=/h2
management.endpoints.web.exposure.include=*


Next Step. Run the application and you will see the application is running successfully but the H2 console not working.




Next Step: To resolve the above issue add H2Config.



To remove the code error please remove scope (runtime) from the h2 dependency:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>


package com.bce;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class H2 {
    private org.h2.tools.Server webServer;
    private org.h2.tools.Server tcpServer;
    @EventListener(org.springframework.context.event.ContextRefreshedEvent.class)
    public void start() throws java.sql.SQLException {
        this.webServer = org.h2.tools.Server.createWebServer("-webPort", "8082", "-tcpAllowOthers").start();
        this.tcpServer = org.h2.tools.Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers").start();
    }

    @EventListener(org.springframework.context.event.ContextClosedEvent.class)
    public void stop() {
        this.tcpServer.stop();
        this.webServer.stop();
    }
}


Next Step: Run again and hit URI (http://localhost:8082)






Download Code from GitHub.  Download

No comments:

Post a Comment