h2-console没有出现

时间:2017-09-26 12:48:08

标签: spring-boot h2

在我的春季启动应用程序中,我添加了以下依赖项:

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

然后在我的application.properties我设置:

spring.h2.console.enabled=true
management.security.enabled=false

但是当我导航到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-console

3 个答案:

答案 0 :(得分:0)

在项目中添加以下配置类,然后重试。

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

答案 1 :(得分:0)

h2-console显示该错误有一个可能的原因。那就是您的文件可能没有正确组织。当我将Controller,Service,Repository和Model文件都放在一个包中时,我也遇到了这个问题。

我已经通过将它们单独包装在如下所示的包中解决了这个问题

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

答案 2 :(得分:0)

在我的案例中,从application.properties中注释掉了春季安全性,这使我能够再次看到h2控制台(重新开始查看效果)->

例如#spring.security.user.name = kk #spring.security.user.password =密码