Spring Boot JPA H2 Console not running, application.properties file ignored
Asked Answered
W

19

20

The Spring Boot guide says I can get the H2 console but it's not working for me.

http://localhost:8080/h2/ Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Oct 26 12:31:46 BST 2016 There was an unexpected error (type=Not Found, status=404). No message available

I created an application.properties file as follows

spring.h2.console.enabled=true 
spring.h2.console.path=/h2

My project is based on this

The default path /h2-console doesn't work either.

I found another answer where the problem is solved by adding to Application.java:

    @Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/h2/*");
    return registration;
}

Everything in my application.properties file is ignored. I have tried adding:

spring.datasource.url=jdbc:h2:file:~/portal;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

But the database is still created in memory only.

Wrote answered 26/10, 2016 at 11:37 Comment(3)
where is your application.properties located and which spring boot version are you using.Nobukonoby
javaworkspace\gs-accessing-data-rest-initial\src\main\java\hello\application.properties and org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASEWrote
that is wrong... It is an non java source so it will be ignored It should go in src\main\resources... Next to that it must be in te root and not a sub package.Nobukonoby
N
8

Your current location src\main\java\h‌​ello\application.pro‌​perties is the culprit. For 2 reasons.

  1. Non java resources in src\main\java are ignored
  2. only application.properties in the root or config directory or taken into account (by default). (See the reference guide).

The fix is to simply move your application.properties to src\main\resources.

Nobukonoby answered 26/10, 2016 at 13:46 Comment(0)
C
23

Check if you set a base path in application.properties.

For example, if you have a setting

server.contextPath=/api

You access the h2 Console under

http://localhost:8080/api/h2-console

Obvious, but that was it for me

Centrepiece answered 5/4, 2017 at 12:3 Comment(1)
Thank you! This was my case!Hysterogenic
P
9

In my case, I just had to remove the tag <scope>runtime</scope> from the h2 dependency as explained in here.

Puttyroot answered 20/1, 2019 at 21:31 Comment(2)
After removing <scope>runtime</scope> , scope changes to <scope>compile</scope> , which is needed to setup spring configurations needed for h2-console.Ayeaye
For me this did not work.Weise
B
9

Another possible cause for this problem, is if you're using spring security. In that case, you may want to add a specific permission to the h2-console URL you defined. For example, for the default h2-console configuration (without a spring.h2.console.path property), you will add this inside your WebSecurityConfigurerAdapter extending class:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/h2-console/**").permitAll()
        .anyRequest().authenticated();
    http.headers().frameOptions().sameOrigin();
}

Please also note that line at the end - http.headers().frameOptions().sameOrigin();. It's needed to prevent a Whitelable page error when logging in to the console. This is described also here.

Backwater answered 14/1, 2020 at 8:9 Comment(0)
N
8

Your current location src\main\java\h‌​ello\application.pro‌​perties is the culprit. For 2 reasons.

  1. Non java resources in src\main\java are ignored
  2. only application.properties in the root or config directory or taken into account (by default). (See the reference guide).

The fix is to simply move your application.properties to src\main\resources.

Nobukonoby answered 26/10, 2016 at 13:46 Comment(0)
Y
7

I tried h2-console while building my microservice project and fell into the same issue "Whitelabel error page". I haven't added any security in my project for now but I found the problem for me is solved by removing spring.h2.console.enabled= true from application.properties and adding dependency dev-tools. for me not adding dev-tools also caused the problem so try to add this dependency as well. while of course, you will add h2, actuator, web dependencies.

Yasui answered 10/12, 2020 at 6:12 Comment(2)
Wowee that worked - the commenting out bit. Thats 2hours of my life I won't get back!!! (I had the dev-tools as well)Conventionalize
I also 'wasted' 2 hours in figuring out, after an Eclipse upgrade (from 2021-12 to 2022-12) causing this error to appear. Adding the dev-tools dependency in my pom.xml and removing the spring.h2.console.enabled did the trick! @Ashish: you're my hero!Foetation
W
5

a / is missing before spring.h2.console.path it have to look like :

spring.h2.console.path=/h2

also when you indicate spring.h2.console.path /h2-console is no more available

Regards

Westfahl answered 26/10, 2016 at 12:22 Comment(4)
Thanks, I tried changing that but it didn't help, see edit.Wrote
have you enough rights to write on ~/portal ? have you tried full path instead of ~ ? any stack trace ? try to but logger=debugWestfahl
If I use the H2 console to access ~/portal then it will create the .mv database files in my home directory but they are blank of course. Created by the console and not by my application. They're both running with the same privilege level.Wrote
maybe you can add .h2.server.properties in resources folder with the following content : #H2 Server Properties 0=JHipster H2 (Disk)|org.h2.Driver|jdbc\:h2\:file\:./target/h2db/db/product|product webAllowOthers=true webPort=8082 webSSL=falseWestfahl
M
4

I found the same case using Eclipse 4.26.0 Did some of above method and not work. Until in one forum, someone suggest us to add devtools dependency.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
</dependency>

I hope it works for you.

Midi answered 7/2, 2023 at 2:6 Comment(0)
G
2

Try the below in application.properties

spring.h2.console.enabled: true
Geodesy answered 17/7, 2019 at 9:49 Comment(2)
I don't know why this comment took -2 voting but this fixed my problem. Thanks!Scaramouch
for me locally it worked without, but on deployment machine this solved my issue, tnxMalda
S
2

try to add to application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

//then create class SecurityConfig

@Configuration
@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
            .authorizeRequests().antMatchers("/h2-console/**").permitAll();
    httpSecurity.csrf().disable();
    httpSecurity.headers().frameOptions().disable();
}

}

Sunn answered 13/5, 2021 at 10:45 Comment(0)
W
1

For me scope=test was the issue as I carelessly copied from https://mvnrepository.com/artifact/com.h2database/h2/1.4.200

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>test</scope>
    </dependency>

And replaced it with

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

After this, it should work with default configurations. enter image description here

I am not using spring security, but you might need to make certain changes accordingly. Please check above answers for that https://mcmap.net/q/609791/-spring-boot-jpa-h2-console-not-running-application-properties-file-ignored

Worse answered 17/5, 2021 at 5:34 Comment(0)
S
1

First, make sure to have these dependencies:

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

Then add these to your application.properties:

 ##spring.main.web-application-type=none make sure it's commented
 spring.h2.console.enabled=true
 spring.h2.console.path=/h2-console
 ##server.port=9999 make sure you use default port (8080). In other words, don't specify a server.port
Spikelet answered 22/6, 2022 at 4:58 Comment(0)
A
0

For me - a computer restart fixed it.

Not sure why this would be the cause but perhaps a port was occupied, or the h2 related files were not deployed correctly

make sure you that when the springBoot application is launched, you can see the log lines for Hibernate and H2 are present:

2017-04-22 14:41:03.195  INFO 912 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-04-22 14:41:03.197  INFO 912 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-04-22 14:41:03.199  INFO 912 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-04-22 14:41:03.278  INFO 912 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-04-22 14:41:03.469  INFO 912 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-04-22 14:41:03.935  INFO 912 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-04-22 14:41:03.945  INFO 912 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
Acker answered 22/4, 2017 at 11:45 Comment(0)
T
0

For this problem, i just add the default string at my application.properties and works.

spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

Maybe the spring boot dont set this for some reason.

Turnery answered 21/9, 2017 at 1:0 Comment(0)
B
0

Remove <scope>test</scope> from h2 dependency in pom.xml works in my case. Don't forget to reinstall and build again after your modification.

<!-- H2 database -->
<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <version>1.4.199</version>
   <!-- <scope>test</scope> -->
</dependency>
Bagby answered 9/9, 2019 at 20:13 Comment(0)
H
0

I used compile otherwise testCompile in the Gradle declaration and was worked fine for me.

Example:

compile group: 'com.h2database', name: 'h2', version: '1.4.200'
Hayley answered 25/12, 2019 at 22:59 Comment(0)
S
0

If you are having troubles with the program ignoring the application.properties and you are using Eclipse, do the following:

Move the application.properties into the src/main/resources folder, right click on it and click "build path" -> "Add to build path"

Sadomasochism answered 6/12, 2020 at 19:35 Comment(0)
A
0

Make sure to use> spring.datasource.url=jdbc:h2:mem:testdb as this db is required for h2

Abhorrence answered 18/3, 2021 at 21:3 Comment(0)
S
0

For me , I just removed the version attribute from the h2 dependency. And then just did maven build clean install.

Shornick answered 15/10, 2023 at 17:36 Comment(0)
G
0
  • try this /h2-ui instead of /h2 in URL
  • Bcoz in new version URL is changed like:
old: http://localhost:9090/h2/login.jsp
new: http://localhost:9090/h2-ui/login.jsp
Gauss answered 6/11, 2023 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.