Spring boot h2控制台不显示
在我的spring boot应用程序中,我添加了以下依赖项:Spring boot h2控制台不显示,spring-boot,h2,Spring Boot,H2,在我的spring boot应用程序中,我添加了以下依赖项: <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.196</version> <scope>test<
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
但当我导航到uri时:
http://localhost:8080/h2-console/login.do?jsessionid=cfc3b5595b531203d92134205e16127e
它抱怨:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Sep 27 03:37:52 GMT-12:00 2017
There was an unexpected error (type=Not Found, status=404).
No message available
为什么我没有访问
h2控制台的权限?在项目中添加以下配置类,然后重试
import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;
@Configuration
public class WebConfiguration {
private static final String mapping = "/console/*";
@Bean
public ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings(mapping);
return registrationBean;
}
@Bean
public FilterRegistrationBean shallowEtagHeaderFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ShallowEtagHeaderFilter());
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
registration.addUrlPatterns(mapping);
return registration;
}
@Bean
public ServletContextInitializer initializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("p-name", "-value");
}
};
}
}
h2控制台
显示该错误可能有一个原因。也就是说,您的文件可能没有以正确的方式组织。当我将控制器、服务、存储库和模型文件放在一个包中时,我也遇到了这个问题
我已经解决了这个问题,将它们分别组织在自己的包中,如下所示
com.example.demo.controller
com.example.demo.service
com.example.demo.repository
com.example.demo.model
我的application.properties文件包含
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
在我的例子中,从application.properties注释出spring安全性使我能够再次看到h2控制台(重新启动以查看效果)-->
例如#spring.security.user.name=kk
#spring.security.user.password=password您的应用程序.properties
中是否有spring.datasource.url
?我今天早些时候进入控制台,但现在出现了相同的错误。也有相同的错误
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect