h2quarkus

How to set H2 Database console url in the Quarkus Application


As Spring boot application provides a property to set the web console URL of the H2 Database.

spring.h2.console.path=/h2

Is there a way to set this same property in the Quarkus application? If not then what is the default web console URL.


Solution

  • Yes, there is a way. But it's not quite as simple as in Spring Boot because Quarkus does not do the same first-class support for H2 as Spring Boot does.

    First, you need to activate Servlet support in Quarkus. Then, you go ahead and configure the H2 servlet in a web.xml deployment descriptor or in a undertow-handlers.conf if you're familiar with it.

    Here we go:

    1. Assuming that you already have the quarkus-jdbc-h2 extension added
    2. Add the quarkus-vertx and quarkus-undertow extensions
    3. Create the deployment descriptor under src/main/resources/META-INF/web.xml
    4. Configure the H2 console Servlet like so
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app
            PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
        <display-name>My Web Application</display-name>
    
        <servlet>
            <servlet-name>h2-console</servlet-name>
            <!-- org.h2.server.web.WebServlet for Quarkus 2 or earlier -->
            <servlet-class>org.h2.server.web.JakartaWebServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>h2-console</servlet-name>
            <url-pattern>/h2/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    Run ./mvnw quarkus:dev and go to http://localhost:8080/h2 where the console should show up.

    If you need to set a parameter use <init-param> like e.g.:

    <servlet>
            <servlet-name>h2-console</servlet-name>
            <servlet-class>org.h2.server.web.JakartaWebServlet</servlet-class>
            <init-param>
                <param-name>webAllowOthers</param-name>
                <param-value>true</param-value>
            </init-param>
        
    </servlet>
    

    http://www.h2database.com/html/tutorial.html#usingH2ConsoleServlet