2017-07-28 104 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

首先嚐試刪除彈簧安全性以進行測試目的,以確定它是否有效 –