0

I downloaded gradle and made new project with pointing to existing gradle file. Now i wanted to connect to H2.database. Here is my build.gradle file:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.0.5.RELEASE'
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
apply plugin: 'application'
apply plugin: 'idea'
group 'shop.hello.manualgradle'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
mainClassName = 'hello.HelloWorld'

repositories {
    mavenCentral()
}

jar {
    baseName = 'gs-gradle'
    version = '0.1.0'
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'joda-time', name: 'joda-time', version: '2.2'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'com.h2database:h2'
    compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'

}

And here is application.properties file which i made both in src/main/resources and in src/test/resources

spring.datasource.name=ecommercedb
spring.jpa.show-sql=true
#H2 settings
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

I run project with gradle idea to update dependencies, then gradlew build and gradlew run. There is no error, but when i go to localhost:8080/h2-console all i get is bad connection. I also tried with gradle build. Why is this happening? I also use IntelIJ and i have build and gradle folders in red, why? I was using this guide: https://www.baeldung.com/spring-angular-ecommerce, but i use own gradle instead of Initializr, and i have Gradle:com.h2.database files in External Libraries so it should work i think! Please, help

λjk.jk
  • 127
  • 1
  • 12
  • when you execute `gradlew run` : do you have so logs that indicates the SpringBoot application has really been started ? can you share some log traces? – M.Ricciuti Oct 15 '18 at 15:59

2 Answers2

0

Your Gradle build looks fine and your application.properties config file is OK as well. The only thing to do to make H2 console available, is to set the property spring.h2.console.enabled=true, like you did: so it should work... You don't need to configure the datasource.url or credentials, Springboot will use default url jdbc:h2:~/test .

You should try to configure H2 db logging level and check if you can see errors/warnings when starting the application or when trying to access the H2 console: add the following property in your application.properties file

spring.datasource.url=jdbc:h2:~/test;TRACE_LEVEL_FILE=4

Then, you should see traces as below when starting your app:

2018-10-15 19:56:25.090 DEBUG 7576 --- [           main] h2database                               : test:jdbc[3] 
/**/conn0.clearWarnings();

When trying to access H2 console you should see traces like that:

 2018-10-15 19:57:21.179 DEBUG 7576 --- [nio-8080-exec-7] h2database                               : test:jdbc[13] Table      :     potential plan item cost 10 000 index meta
M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
  • M.Ricciuti Thats weird because i dont see any trace. All i see is Build Successfull, i do gradle build, then gradle run, it passed without problem printing Hello World, but when i go to localhost:8080/h2-console or /console there is error connection - still no trace in Intelij Terminal. I also tried Khwaja Sanjari, and pasted code from thread he posted i even made new Application class with ServletRegistrationBean - still: no error, no console on localhost, nothing – λjk.jk Oct 15 '18 at 21:38
0

I was running into the same issue and found that I had trailing spaces after spring.h2.console.path=/h2-console in my application.properties file. This was causing it to route incorrectly. This is a pretty old question but I figured I'd add what fixed the problem for me.