How To enable h2 console in java ee application

Viewed 1188

I would like to have access to h2 console feature during development of my app. I am using JavaEE and wildfly as my application server.

I know that for spring boot we need to add follwing lines of configuration:

spring.h2.console.enabled=true

spring.h2.console.path=/h2

and than we have console at http://localhost:8080/h2/

But what about javaEE? How Can I access it? My pom has followin dependency to h2:

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

and persistance.xml:

<properties>
    <property name="eclipselink.logging.level" value="INFO"/>
    <property name="eclipselink.logging.parameters" value="true"/>

    <property name="hibernate.show_sql" value="true" />
    <property name="javax.persistence.schema-generation.database.action" value="create"/>
    <property name="javax.persistence.sql-load-script-source" value="META-INF/initial.sql"/>
</properties>
1 Answers

If you're deploying Spring Boot on the JEE server, the console should be at the defined path under the application context path.

However, if you're asking for a solution that doesn't rely on Spring Boot, there is an org.h2.server.web.WebServlet that fits exactly this case. All that is needed is to expose it using the WEB-INF/web.xml deployment descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
  <servlet>
    <servlet-name>h2-console</servlet-name>
    <servlet-class>org.h2.server.web.WebServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>h2-console</servlet-name>
    <url-pattern>/h2/*</url-pattern>
  </servlet-mapping>
</web-app>
Related