且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法查看h2数据库Web控制台以及如何更改默认的h2端口

更新时间:2023-01-18 15:44:32

使用Spring Boot,您可以像这样配置H2控制台servlet:

With Spring Boot, you can configure your H2 console servlet like this:

(提示:确保导入了正确的库)

(hint: make sure the right libraries are imported)

import org.h2.server.web.WebServlet;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
    @Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registration = new ServletRegistrationBean( new org.h2.server.web.WebServlet());
        registration.addUrlMappings("/h2-console/*");
        registration.addInitParameter("webAllowOthers", "true");
        registration.addInitParameter("webPort", "7777");// <-- the port your wish goes here

        return registration;
    }
}

然后,您应该可以通过URL http://localhost:7777/h2-console

Then you should be able to access H2 via your URL http://localhost:7777/h2-console

(借助Spring Guru H2控制台设置)