user3123690
user3123690

Reputation: 1241

Spring boot H2 console returns 404

I have a simple Springboot application and it's up running. I am able to call REST endpoints through Postman. However, when I try to access the console using http://localhost:8080/h2 it keeps returning 404.


2020-08-29 08:06:37.577  INFO 6507 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [

        name: default

        ...]

2020-08-29 08:06:37.644  INFO 6507 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}

2020-08-29 08:06:37.645  INFO 6507 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found

2020-08-29 08:06:37.677  INFO 6507 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

2020-08-29 08:06:37.787  INFO 6507 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

2020-08-29 08:06:38.247  INFO 6507 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2020-08-29 08:06:38.737  INFO 6507 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

2020-08-29 08:06:38.738  INFO 6507 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure

2020-08-29 08:06:38.741  INFO 6507 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]

2020-08-29 08:06:38.813  INFO 6507 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext     : Started HttpServer on /0:0:0:0:0:0:0:0:8080

2020-08-29 08:06:38.813  INFO 6507 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080

2020-08-29 08:06:38.816  INFO 6507 --- [           main] c.v.t.c.m.tenant.discovery.Application   : Started Application in 3.4 seconds (JVM running for 3.661)

2020-08-29 08:06:48.527  WARN 6507 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/h2]: Response status 404

2020-08-29 08:23:07.301  WARN 6507 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Retrograde clock change detected (housekeeper delta=29s626ms), soft-evicting connections from pool.

2020-08-29 08:57:06.586  WARN 6507 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=33m59s284ms).

2020-08-29 09:35:49.699  WARN 6507 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper 

My application.yml file looks like this.

spring:
  datasource:
    url: jdbc:h2:mem:test
    platform: h2
    username: sa
    password:
    driverClassName: org.h2.Driver
    hikari:
      maximum-pool-size: 50
  h2:
    console:
      enabled: true
      path: /h2
      settings:
        web-allow-others: true
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
server:
  port: 8080

Upvotes: 5

Views: 6136

Answers (1)

Dinesh Dontha
Dinesh Dontha

Reputation: 579

As per your logs, I found that, you are using an embedded server, other than tomcat, i.e, spring-boot-starter-reactor-netty that comes along with spring webflux dependency.

H2ConsoleAutoConfiguration will not be executed for spring webflux & netty(reactor based), as H2 console will only be available to servlet based applications. So, you have to configure H2 server manually in this spring boot application, with spring webflux & netty.

Sample H2Server (working code):

package com.example.springbootnettyserver;

import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import static org.h2.tools.Server.createWebServer;

    @Component
    public class H2ServerManual {
    
            Logger log = LoggerFactory.getLogger(H2ServerManual.class);
            private Server webServer;
    
            @Value("${h2-server.port}")
            Integer h2ConsolePort;
    
            @EventListener(ContextRefreshedEvent.class)
            public void start() throws java.sql.SQLException {
                log.info("starting h2 console at port "+ h2ConsolePort);
                this.webServer = createWebServer("-webPort", h2ConsolePort.toString(),
                                                 "-tcpAllowOthers").start();
                System.out.println(webServer.getURL());
            }
    
            @EventListener(ContextClosedEvent.class)
            public void stop() {
                log.info("stopping h2 console at port "+h2ConsolePort);
                this.webServer.stop();
            }
    }

Note: org.h2.tools.Server class will be resolved only when h2 dependency is added without scope of runtime. (remove scope element in dependency)

In application.properties, you will add port details for h2 server like

h2-server:
 port: 8081

Here, now, the H2 console will be available at http://localhost:8081

Following properties can be removed

#  h2:
#    console:
#      enabled: true
#      path: /h2
#      settings:
#        web-allow-others: true

For Reference: https://github.com/donthadineshkumar/webflux-netty-server-h2-example.git

Upvotes: 10

Related Questions