To get an H2 Database Web Console accessing an in-memory database running on Spring Boot:
- Register H2’s WebServlet (with UrlMapping /console/*) to your Spring project in a Configuration annotated class. Restart your Application.
- Open a web page to http://localhost:8080/console/
- 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;
}
}