H2控制台 Spring Boot 未打开

0qx6xfy6  于 2023-03-02  发布在  Spring
关注(0)|答案(2)|浏览(224)

我有非常简单的项目POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>jar</packaging>
    <groupId>com.example</groupId>
    <artifactId>managing-transactions</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>managing-transactions</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>

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

在www.example.com中appliation.properties我尝试了不同的变体

#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

但控制台未在http://localhost:8080/h2-console上打开
唯一的办法就是用老办法

Runnable runServer = new Runnable() {
    @Override
    public void run() {
        try {
            Server.startWebServer(DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", ""));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
};
Thread threadServer = new Thread(runServer);
threadServer.start();
cu6pst1q

cu6pst1q1#

要使用Sping Boot 项目查看H2控制台,需要运行Web服务器,以便提供“h2-console”url。
因此,不要单独使用JDBC项目:

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

同时添加starter-web项目。

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

然后,在www.example.com文件中显式配置要启用的H2控制台application.properties,或者将Sping Boot Dev Tools依赖项添加到您的pom.xml,这将隐式配置要启用的H2控制台。
application.properties:
spring.h2.console.enabled=true
pom.xml中的Sping Boot 开发工具依赖项。

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

下面是演示该功能的最少代码。它是一个仅由这一个类组成的应用程序。

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MinimumSpringBootWithWebForH2 {

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

}

下面是这个最小示例的控制台输出:

下面是一个示例项目的链接:https://github.com/shankarps/H2Project
这里有一篇文章,有一些细节:https://medium.com/@harittweets/how-to-connect-to-h2-database-during-development-testing-using-spring-boot-44bbb287570
以下是Sping Boot 文档的链接,这些文档指定了为您的应用提供H2控制台的要求:https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.sql.h2-web-console

a9wyjsp7

a9wyjsp72#

您还可以查看我的pom.xml,它是在Java 11中构建和运行的。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.remad</groupId>
    <artifactId>JPAStarter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JPAStarter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </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>
    </dependencies>

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

</project>

application.properties

###############################################################################
# Spring database source config
###############################################################################
spring.datasource.url=jdbc:h2:file:/tmp/JpaStarter/db
#spring.datasource.username=sa
#spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver

###############################################################################
# Hibernate Configuration
###############################################################################
spring.jpa.hbernate.ddl-auto = update

###############################################################################
# Debug configuration
###############################################################################
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

希望这对你有帮助:-)

相关问题