【问题标题】:Can't access the H2 database console using SpringBoot无法使用 Spring Boot 访问 H2 数据库控制台
【发布时间】:2020-12-15 08:15:49
【问题描述】:

我有基本的 SpringBoot 应用程序。我遵循本教程:https://www.baeldung.com/spring-boot-h2-database 从头开始​​创建所有内容,但是当我完成所有步骤并尝试访问 h2 控制台时。它显示“无法访问此站点”。首先我尝试了默认路径:http://localhost:8080/h2-console,然后我添加到我的 application.properties 文件-server.contextPath="api" 和 server.port=8090,以确保端口未使用,但仍无法访问 h2 控制台。

这是我的 pom.xml 文件:

<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>

这是我的 application.properties 文件:

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

主类是默认类:

@SpringBootApplication
public class AgriculturerentApplication {

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

}

您能告诉我访问 h2 控制台的问题在哪里吗?

提前致谢。

【问题讨论】:

  • 它是否适用于 /console 而不是 /h2-console?我遇到了类似的问题,并且为我做到了。此外,如果您使用 Spring Security,您可能需要允许不受限制地访问该端点。
  • @JustAnotherDeveloper 不,它不能仅与“/console”一起使用。我没有使用 Spring Security。

标签: java spring-boot maven h2 application.properties


【解决方案1】:

您使用 application.propiertes 中定义的端口和上下文访问 H2 控制台。

例如,如果您定义:

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

您访问 h2-console:http://localhost:8090/api/h2-console

问候。

【讨论】:

  • 正如我上面所说,它也无济于事。问候
【解决方案2】:

可能您没有服务器,因此无法打开任何 localhost 页面。

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/ 一切正常,检查

【讨论】:

  • 我又遇到了同样的问题,即使我在 pom.xml 中添加了这种依赖关系。
  • 再次检查,我在本地主机上运行它 - 一切正常。也许你没有更新你的 maven 项目?
  • 这很有趣,它确实对你有用,因为对我来说仍然无法打开 h2-console。
  • 添加新依赖后你更新了maven项目了吗?
  • 好吧,也许我没有重新导入但仍然无法打开页面,但现在错误是: Whitelabel Error Page 此应用程序没有显式映射 /error,因此您将此视为后备。 2020 年 8 月 26 日星期三 17:45:57 EEST 出现意外错误(类型=未找到,状态=404)。
【解决方案3】:

我遇到了同样的问题,我解决这个问题的方法是,要为 spring boot 添加开发工具,只需将其添加到 com.h2database 的 pom.xml 中即可。

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

出于一个奇怪的原因,我刚刚添加了“devtools”并且它有效。

如果我删除它并执行 Project->clean 问题再次发生。

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2022-01-18
    相关资源
    最近更新 更多