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 here. How 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 propertyspring.application.name = reactive-basicserver.port=8080spring.webflux.base-path=/api# H2-database propertyspring.datasource.driverClassName=org.h2.Driverspring.datasource.username=saspring.datasource.password=spring.jpa.database-platform=org.hibernate.dialect.H2Dialect#enabling the H2 consolespring.h2.console.enabled=truespring.jpa.show-sql=truespring.jpa.generate-ddl=truespring.jpa.hibernate.ddl-auto=updatespring.datasource.url=jdbc:h2:C:/data/reactive# Custom H2 Console URLspring.h2.console.path=/h2management.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;@Componentpublic 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