SpringBoot Accessing H2 console

2020-02-29 03:31发布

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;
    }

but when 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:

enter image description here

5条回答
姐就是有狂的资本
2楼-- · 2020-02-29 04:06

Remove all you have in your properties file. All of those you mentioned are default. Springboot will configure it any way as soon as it identifies h2 dependency in your pom. And also you dont 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

查看更多
爷、活的狠高调
3楼-- · 2020-02-29 04:06

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

查看更多
男人必须洒脱
4楼-- · 2020-02-29 04:12

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
查看更多
三岁会撩人
5楼-- · 2020-02-29 04:29

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

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

in http.authorizeRequests()

查看更多
beautiful°
6楼-- · 2020-02-29 04:31

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

查看更多
登录 后发表回答