LuXuS
LuXuS

Reputation: 71

Can't access the H2 database console using SpringBoot

I have basic SpringBoot application. And i follow this tutorial: https://www.baeldung.com/spring-boot-h2-database for creating all from scratch, but when i finish all the steps and try to access the h2-console. It shows me "This site can’t be reached". Firstly i've tried the default path: http://localhost:8080/h2-console, then i added to my application.properties file- server.contextPath="api" and server.port=8090, to be sure that the port is not used but still doesn't have access to the h2 console.

Here is my pom.xml file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>gtech</groupId>
<artifactId>agriculturerent</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.20.Final</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

This is my application.properties file:

spring.datasource.url=jdbc:h2:file:./TestDb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=""
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.h2.console.enabled=true

And the main class is default one:

@SpringBootApplication
public class AgriculturerentApplication {

    public static void main(String[] args) {
        SpringApplication.run(AgriculturerentApplication.class, args);
    }

}

Could you please tell me where i am wrong with accessing the h2 console?

Thanks in advance.

Upvotes: 2

Views: 18948

Answers (6)

Shiva Harsha
Shiva Harsha

Reputation: 1

Add devtools to the project, it worked for me....right click on the project -> spring -> add dev tools

Upvotes: 0

Einstein_AB
Einstein_AB

Reputation: 436

For me I was missing (As mentioned by @Manish Zacharias.)

Don't forget to add spring.h2.console.enabled=true in your application.properties.

Upvotes: 1

Manish Zacharias
Manish Zacharias

Reputation: 87

I had the same problem too. This was what I had to add to my pom.xml to access the H2 console:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

Don't forget to add spring.h2.console.enabled=true in your application.properties.

Make sure you access the H2 database from your browser using the URL: http://localhost:8080/h2-console/

Make sure you are running the Spring application.

Note: I don't have Spring Boot Dev Tools dependecy in my pom.xml.

Upvotes: 4

Efren Robles
Efren Robles

Reputation: 161

I was in the same issue, and the way that I solve this was, to add the dev tools for spring boot, just add this in your pom.xml above of com.h2database.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

For a strange reason, I just added the "devtools" and that works.

If I remove it and do Project->clean the issue happens again.

Upvotes: 5

Taras
Taras

Reputation: 469

Probably you don't have server so you can't open any localhost page.

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.20.Final</version>
        </dependency>
    </dependencies>

application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

http://localhost:8080/h2-console/ all works fine, checked

Upvotes: 4

Deusimar Ferreira
Deusimar Ferreira

Reputation: 495

You access H2 console with port and context that defined in your application.propiertes.

Example, if you defined:

server.port=8090
server.servlet.context-path=api

You access h2-console in: http://localhost:8090/api/h2-console

Regards.

Upvotes: 5

Related Questions