2017-07-28 82 views
1

我在我的应用程序属性文件中使用了下面的设置。但仍然无法在h2控制台中看到我的表格。从H2连接到H2数据库控制台

enter image description here

application.properties

logging.level.org.springframework.web=INFO 
spring.datasource.url=jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE 
spring.datasource.driver-class-name=org.h2.Driver 
spring.datasource.schema=classpath:/schema.sql 
spring.datasource.data=classpath:/data.sql 
spring.h2.console.enabled=true 
spring.h2.console.path=/h2 
server.port = 8080 

我使用了字符串jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE在JDBC URL H2控制台上登录。然而,我没有看到任何表

下面是我Graddle文件

buildscript { 
    ext { 
     springBootVersion = '1.4.4.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'org.springframework.boot' 

jar { 
    baseName = 'challenge' 
    version = '0.0.1-SNAPSHOT' 
} 

sourceCompatibility = 1.8 

repositories { 
    mavenCentral() 
} 


dependencies { 
    compile('org.springframework.boot:spring-boot-starter-jdbc') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    runtime('org.springframework.boot:spring-boot-devtools') 
    runtime('com.h2database:h2') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

以下是实现Springsecurity我的春天启动的应用程序

@Configuration 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests().antMatchers("/h2/*").permitAll(); 


     http.csrf().disable(); 
     http.headers().frameOptions().disable(); 


    } 
} 
+0

[查看H2或HSQLDB内存数据库的内容]的可能重复(https://stackoverflow.com/questions/7309359/view-content-of-h2-or-hsqldb-in-memory-database) –

+0

感谢您的意见。但我确实阅读了所有其他文章,但他们都没有解决我的问题。我已经启动了我的Spring启动应用程序,它基本上是一个连接到H2数据库并获取所有信息的Web服务。 web服务正在显示信息,但是当我登录到H2控制台时,我没有看到任何表或数据库 – Vinayak

+0

请显示您的pom或gradle –

回答

1

类的H2引擎提供了一个控制台在那里你可以看到所有的表格及其数据。这个控制台是一个Web应用程序。所以,你需要访问H2控制台的是将spring-boot-starter-web pom依赖包括到你的pom.xml中。

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

您还需要将以下属性添加到src/main/resources/application.properties文件。

spring.h2.console.enabled=true 

的H2 Web控制台可以在这里(默认链接)访问:http://localhost:8080/h2-console

enter image description here

您应该看到驱动程序类的JDBC URL和 凭据。如果JDBC URL不同,请将其值修改为jdbc:h2:mem:yourdbName

现在,如果你在你的项目中弹簧引导启动安全依赖线需要被添加到SecurityConfig的项目中的配置方法,否则就会登录到后看到一个空白页H2控制台:

http.headers()。frameOptions()。disable(); 。

可替换地,下面的行,可以使用:

http.headers()frameOptions()SAMEORIGIN();

+0

我已经完成了上述提到包括以下内容:spring.h2.console.enabled = true(Application.properties文件)也包括gradle中的spring-boot-starter-web。但我仍然面临这个问题 - – Vinayak

+0

我已经更新了我的回答 –

+0

首先尝试删除弹簧安全性以进行测试目的,以确定它是否有效 –