H2 Web Console to In Memory Database – Spring Boot

To get an H2 Database Web Console accessing an in-memory database running on Spring Boot:

  1. Register H2’s WebServlet (with UrlMapping /console/*) to your Spring project in a Configuration annotated class. Restart your Application.
  2. Open a web page to http://localhost:8080/console/
  3. Be sure you’re accessing the correct database JDBC URL (default would be jdbc:h2:mem:testdb)

(Credit: Spring Framework Guru)

 

import org.h2.server.web.WebServlet;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class H2ConsoleWebConfiguration {
 @Bean
 ServletRegistrationBean h2servletRegistration() {
 ServletRegistrationBean registrationBean = new ServletRegistrationBean(new WebServlet());
 registrationBean.addUrlMappings("/console/*");
 return registrationBean;
 }
}

Posted

in

,

by

Tags: