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:
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 filespring.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
propertyFirst 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:
You can access the console via
Secondly, Always use the ddl-auto property as "update" rather than "create" because create will delete existing schema.
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:
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 usinghttp://localhost:8080/h2
Now if you have implemented SecurityConfig in application then you will need to add
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 belowWith 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:
POM: spring-boot-starter, h2, spring-boot-starter-web
This was the case for Spring Boot 2.1.1 might helpful to others