简体   繁体   中英

SpringBoot Accessing H2 console

I have a basic SpringBoot app., embedded Tomcat, Thymeleaf template engine. I've created this bean to access the console:

@Bean
public ServletRegistrationBean h2ConsoleServletRegistration() {
    ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
    bean.addUrlMappings("/console/*");
    return bean;
}

I access to the console: http://localhost:8080/appContext/console/login.do?jsessionid=f3585792a9bf1f0cf1a0b6a09dcefe1a

I have my beans annotated as follows:

@Entity
@Table(name="t_user")
public class User implements Serializable, UserDetails {
..
}

My application properties:

# Spring Data JPA properties

spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

hibernate.dialect=org.hibernate.dialect.H2Dialect

But I don't see any table created by JPA:

在此处输入图像描述

Remove all you have in your properties file. All of those you mentioned are default. Spring-boot will configure it any way as soon as it identifies h2 dependency in your pom. And also you don't need that ServletRegistration bean. Remove that as well. Just put this in your properties file spring.h2.console.enabled=true .

By default console can be accessed on http://localhost:8080/h2-console , default path is h2-console . You can configure it using spring.h2.console.path property.

We only need below configuration in application.properties file:

spring.h2.console.enabled=true

By default h2 will be available at http://localhost:8080/h2-console/

But one can define spring.h2.console.path=/h2 in application.properties and after that h2 can be accessed using http://localhost:8080/h2 .

Now if you have implemented SecurityConfig in application then you will need to add

// Make H2-Console non-secured; for debug purposes
.and().csrf().ignoringAntMatchers("/h2/**")
// Allow pages to be loaded in frames from
// the same origin; needed for H2-Console
.and().headers().frameOptions().sameOrigin()

in http.authorizeRequests()

We can access the H2 console with default path as http://localhost:8080/h2-console if we have devtools dependency in pom.xml else we have to specify the path for H2 in an application.properties as below


With devtools can be accessed at http://localhost:8080/h2-console/

POM: spring-boot-starter, h2, spring-boot-starter-web, spring-boot-devtools

Without devtools - we need to set it in properties:

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

POM: spring-boot-starter, h2, spring-boot-starter-web

This was the case for Spring Boot 2.1.1 might helpful to others

First of all, you do not have to explicitly define a bean to access H2 Console. It is taken care by the Springboot already. You can define the H2 Console path in your application.properties like below:

spring.h2.path = /h2-console

You can access the console via

http://host:port/h2-console

Secondly, Always use the ddl-auto property as "update" rather than "create" because create will delete existing schema.

spring.jpa.hibernate.ddl-auto=update

If you're looking for a starter project of Spring boot and H2 (with Hiberante Enver as bonus, remove the enver package and @Audited from the entity if you do not want that) - You can try the below one:

https://github.com/sundarsy/springboot-h2-enver

Have a look at: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

Try setting this property:

spring.jpa.hibernate.ddl-auto=create

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM