Sal-laS
Sal-laS

Reputation: 11659

h2-console does not show up

In my spring boot application, I have added the below dependencies:

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

and then in my application.properties i set:

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

But when i navigate to the uri:

http://localhost:8080/h2-console/login.do?jsessionid=cfc3b5595b531203d92134205e16127e

It complains with:

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

Why don't i have access to h2-console?

Upvotes: 4

Views: 8595

Answers (3)

kishorK
kishorK

Reputation: 543

in my case commenting out spring security from application.properties allowed me to see h2 console again(restart to see the effect)-->

ex. #spring.security.user.name=kk #spring.security.user.password=password

Upvotes: 0

Rakib Hasan
Rakib Hasan

Reputation: 21

There is one possible reason why h2-console show that error. That is your files may not be organized in right way. I've also fetched this problem when I keep my Controller, Service, Repository and Model files all together in one package.

I've solved this problem by organizing them separately in their own packages like below

com.example.demo.controller
com.example.demo.service
com.example.demo.repository
com.example.demo.model

My application.properties file contains

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

Upvotes: 2

mrtasln
mrtasln

Reputation: 614

Add following configuration class in your project and try again.

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

Upvotes: 0

Related Questions