无法查看h2数据库Web控制台以及如何更改默认的h2端口 [英] not able to view h2 database web console and how to change the default h2 port

查看:139
本文介绍了无法查看h2数据库Web控制台以及如何更改默认的h2端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是H2数据库的新手.出于开发目的,我想使用H2数据库,但是在Spring Boot Web应用程序中配置它时遇到了麻烦.我已经浏览了一些教程和SO线程,但是没有一个可以解决我的问题.以下是我已经完成但没有成功的线索.

I am new to H2 database. For the development purpose I would like to use H2 database and I am having trouble in configuring it with my spring boot web application. I have gone thru several tutorials and SO threads but none could solve my issue. Below are the trails that I have done and none was successful.

要求:1.我可以使用Web界面查看的H2数据库(控制台视图)2.即使停止正在运行的Web应用程序甚至是机器(笔记本电脑),我也希望数据能够保留下来,这样我就不必再次输入所有数据并可以从离开的地方开始.3.想要将控制台的默认端口(8080)更改为我自己选择的(XXXX).

Requirement: 1. H2 data base that I can view with web interface( console view) 2. I would like my data to be persisted even if I stop my running web application or even my machine(laptop) so that I do not have to enter all the data once again and can start from where I have left. 3. Would like to change the default port of console(8080) to my own choice(XXXX).

在查看 H2功能后,尝试在具有TCP的服务器模式下进行尝试,但无法成功启动我的应用程序,无法查看Console(web)应用程序,也无法将默认端口更改为我自己选择的端口.

After viewing H2 Features Tried with Server Mode with TCP but was not able to successfully start my application , was not able to view Console(web) application and was not able to change the default port to my own choice of port.

pom.xml:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javasree</groupId>
 <artifactId>familytree.thymeleaf</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>familytree</name>
 <packaging>jar</packaging>

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- <version>1.5.2.RELEASE</version> -->
    <version>2.0.0.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <hibernate.version>5.2.3.Final</hibernate.version>
    <start- class>com.javasree.spring.familytree.FamilyTreeApplication</start-class>
</properties>

<dependencies>
    <!-- This is a web application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- This is a web application test dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring- boot-starter-data-jpa -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- hot swapping, disable cache for template, enable live reload -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- Tomcat embedded container-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP,
        tomcat-embed-jasper version is not working, no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- hibernate dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

<!--        <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.0</version>
    </dependency> -->

<!--        <dependency>
        <groupId>commons-digester</groupId>
        <artifactId>commons-digester</artifactId>
        <version>2.0</version>
    </dependency> -->

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <!--<version>5.1.36</version>-->
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

    <!-- Optional, for bootstrap -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
    </dependency>

    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/oracle/ojdbc6
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency> -->

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

</dependencies>

<repositories>
    <repository>
        <id>codelds</id>
        <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
</repositories>

<build>
    <plugins>
        <!-- Package as an executable jar/war -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
      <dependencies>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-filtering</artifactId>
            <version>1.3</version>
        </dependency>
      </dependencies>
        </plugin>
    </plugins>

  </build>
</project> 

Spring引导类:

Spring boot class:

@SpringBootApplication
@EntityScan(basePackages={"com.javasree.spring.familytree.model"})
@EnableJpaRepositories(basePackages = { 
 "com.javasree.spring.familytree.web.jpa"})
public class FamilyTreeApplication extends SpringBootServletInitializer{

 @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application){
    return application.sources(FamilyTreeApplication.class);
}
public static void main(String[] args) {
    try {
        Server.createTcpServer("-tcpAllowOthers","-webAllowOthers").start();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    new SpringApplicationBuilder(FamilyTreeApplication.class)
    //.bannerMode(Mode.CONSOLE)
    .build().run(args);
}
}

application.properties:

application.properties:

    server.contextPath=/familytree
    server.port=7030

    # JPA properties
     spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
    spring.data.jpa.repositories.enabled=true
    spring.jpa.properties.hibernate.use_sql_comments=true

    spring.jpa.show-sql=true

    spring.jpa.generate-ddl=false
    spring.jpa.properties.hibernate.type=trace 


    #H2 config
    spring.h2.console.enabled=true
    spring.h2.console.path=/h2
    spring.h2.console.settings.trace=false
    # Datasource
     spring.datasource.url=jdbc:h2:tcp://localhost:8084/~/test;MODE=MYSQL;AUTO_SERVER=TRUE;
    spring.datasource.username=sa
    spring.datasource.password=
    spring.datasource.driver-class-name=org.h2.Driver
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

我得到的异常:

    org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost:8084" [90067-196]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
        at org.h2.message.DbException.get(DbException.java:168)
        at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:457)
        at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:334)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:116)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:100)
        at org.h2.Driver.connect(Driver.java:69)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:117)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:123)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:365)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)

让我知道是否需要发布更多代码或任何其他详细信息以更好地理解该问题.

Let me know if it's required to post any more code or any other details to better understand the issue.

推荐答案

使用Spring Boot,您可以像这样配置H2控制台servlet:

With Spring Boot, you can configure your H2 console servlet like this:

(提示:确保导入了正确的库)

(hint: make sure the right libraries are imported)

import org.h2.server.web.WebServlet;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
    @Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registration = new ServletRegistrationBean( new org.h2.server.web.WebServlet());
        registration.addUrlMappings("/h2-console/*");
        registration.addInitParameter("webAllowOthers", "true");
        registration.addInitParameter("webPort", "7777");// <-- the port your wish goes here

        return registration;
    }
}

然后,您应该可以通过URL http://localhost:7777/h2-console

Then you should be able to access H2 via your URL http://localhost:7777/h2-console

(借助Spring Guru H2控制台设置)

这篇关于无法查看h2数据库Web控制台以及如何更改默认的h2端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆