英文:
Can't access the H2 database console using SpringBoot
问题
这是我翻译好的部分:
我有一个基本的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>
<!-- 其他依赖 -->
</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控制台方面做错了什么吗?提前谢谢。
英文:
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>
</project>
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.
答案1
得分: 5
我也遇到了相同的问题,我解决的方法是在Spring Boot的开发工具中添加以下内容到你的 pom.xml 文件中 com.h2database 之前。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
出奇的是,我只是添加了 "devtools" 就解决了。
如果我将它移除然后执行项目->清理操作,问题会再次出现。
英文:
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.
答案2
得分: 4
你可以通过应用程序属性中定义的端口和上下文访问 H2 控制台。
例如,如果你定义了:
server.port=8090
server.servlet.context-path=api
你可以通过以下链接访问 h2-console:http://localhost:8090/api/h2-console
祝好。
英文:
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.
答案3
得分: 3
可能您没有服务器,因此无法打开任何本地主机页面。
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/
一切正常,已验证
英文:
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
答案4
得分: 1
我也遇到了同样的问题。这是我必须在我的 pom.xml
中添加以访问 H2 控制台的内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
不要忘记在你的 application.properties
中添加 spring.h2.console.enabled=true
。
确保你使用以下 URL 从浏览器访问 H2 数据库:http://localhost:8080/h2-console/
确保你正在运行 Spring 应用程序。
注意:我在我的 pom.xml
中没有 Spring Boot Dev Tools 依赖。
英文:
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
.
答案5
得分: 0
以下是翻译好的内容:
对我来说,我遗漏了(正如@Manish Zacharias所提到的。)
> 不要忘记在你的application.properties中添加spring.h2.console.enabled=true。
英文:
For me I was missing (As mentioned by @Manish Zacharias.)
> Don't forget to add spring.h2.console.enabled=true in your application.properties.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论