【发布时间】: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