Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 214
clickcell Avatar asked Oct 26 '16 11:10

clickcell


3 Answers

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

like image 154
Falco Winkler Avatar answered Sep 30 '22 04:09

Falco Winkler


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.

like image 20
M. Deinum Avatar answered Sep 30 '22 03:09

M. Deinum


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.

like image 34
saar schweid Avatar answered Sep 30 '22 04:09

saar schweid