如何使用 Netbeans Spring-Boot 项目访问 h2-console

Posted

技术标签:

【中文标题】如何使用 Netbeans Spring-Boot 项目访问 h2-console【英文标题】:How to access h2-console with a Netbeans Spring-Boot project 【发布时间】:2017-12-24 00:39:32 【问题描述】:

我正在使用 spring boot 1.5.4,我正在关注一个教程,其中显示只需将 h2-console 添加到 localhost:8080/ url您可以访问控制台。但是当我这样做时,我有一个 404 Whitelabel 错误。 这些是依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--WebJars-->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.5</version>
     </dependency>
     <dependency>
         <groupId>org.webjars</groupId>
         <artifactId>jquery</artifactId>
         <version>3.2.1</version>
     </dependency>

     <!--jpa and database-->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
     <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <scope>runtime</scope>
     </dependency>

</dependencies>

这是 Spring Boot 控制台日志

2017-07-19 01:35:25.222  INFO 8644 --- [           main] c.e.s.SpringBootTest00Application        : Starting SpringBootTest00Application on DESKTOP-K8Q0B2R with PID 8644 (started by Talon in C:\Users\Talon\Desktop\java netbeans\01\springBootTest00)
2017-07-19 01:35:25.225  INFO 8644 --- [           main] c.e.s.SpringBootTest00Application        : No active profile set, falling back to default profiles: default
2017-07-19 01:35:25.544  INFO 8644 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@37574691: startup date [Wed Jul 19 01:35:25 CEST 2017]; root of context hierarchy
2017-07-19 01:35:27.236  INFO 8644 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-19 01:35:27.248  INFO 8644 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-07-19 01:35:27.249  INFO 8644 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-19 01:35:27.378  INFO 8644 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-07-19 01:35:27.378  INFO 8644 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1838 ms
2017-07-19 01:35:27.578  INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-19 01:35:27.582  INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-19 01:35:27.584  INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-19 01:35:27.584  INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-19 01:35:27.584  INFO 8644 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-19 01:35:28.020  INFO 8644 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-07-19 01:35:28.037  INFO 8644 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-07-19 01:35:28.122  INFO 8644 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core 5.0.12.Final
2017-07-19 01:35:28.123  INFO 8644 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-07-19 01:35:28.180  INFO 8644 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-07-19 01:35:28.219  INFO 8644 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2017-07-19 01:35:28.321  INFO 8644 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-07-19 01:35:28.742  INFO 8644 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-07-19 01:35:28.757  INFO 8644 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2017-07-19 01:35:28.809  INFO 8644 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-07-19 01:35:29.119  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@37574691: startup date [Wed Jul 19 01:35:25 CEST 2017]; root of context hierarchy
2017-07-19 01:35:29.203  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/]" onto public java.lang.String com.example.springBootTest00.controllers.IndexController.index()
2017-07-19 01:35:29.205  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/product],methods=[POST]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.saveOrUpdateProduct(com.example.springBootTest00.domain.Product)
2017-07-19 01:35:29.206  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/product/edit/id]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.edit(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.206  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/product/new]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.newProduct(org.springframework.ui.Model)
2017-07-19 01:35:29.206  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/product/delete/id]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.deleteProduct(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.206  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/products]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.listProducts(org.springframework.ui.Model)
2017-07-19 01:35:29.206  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/product/id]" onto public java.lang.String com.example.springBootTest00.controllers.ProductController.getProduct(java.lang.Integer,org.springframework.ui.Model)
2017-07-19 01:35:29.209  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-07-19 01:35:29.209  INFO 8644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-07-19 01:35:29.243  INFO 8644 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.244  INFO 8644 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.284  INFO 8644 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-19 01:35:29.920  INFO 8644 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-19 01:35:29.983  INFO 8644 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-19 01:35:29.987  INFO 8644 --- [           main] c.e.s.SpringBootTest00Application        : Started SpringBootTest00Application in 5.052 seconds (JVM running for 5.401)

我有一些 @Service / @Controller 正常工作的类,所有请求都映射到 h2-console 以外的 url 字符串。我应该在 pom 中导入其他东西还是在 application.properties 中配置一些东西?

【问题讨论】:

【参考方案1】:

简单的写作步骤:

Application.properties文件中包含

spring.h2.console.path=/h2
spring.h2.console.enabled=true

并且在pom.xml 中包含devtools 依赖

       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <optional>true</optional>
      </dependency>

重新打包并打开http://localhost:[port]/h2你已经准备好了

您必须包含的其他属性是

spring.datasource.url=jdbc:h2:file:~/h2db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

文件h2db将存储在windows的userprofile文件夹中 例如:C:\Users\[profile]

【讨论】:

到目前为止,在 application.properties 中添加这些行似乎已经完成了这项工作。如果我在此过程中发现任何问题,将包括其他内容。非常感谢。

以上是关于如何使用 Netbeans Spring-Boot 项目访问 h2-console的主要内容,如果未能解决你的问题,请参考以下文章

如何设置 NetBeans 项目属性以使用 Java 7?

如何将 Angular 2 与 NetBeans 一起使用?

netbeans如何使用图片做界面背景

如何使用 Netbeans 连接到远程 tomcat?

如何在 NetBeans 中使用 Microsoft C++ 编译器?

如何在 NetBeans 8.2 中使用 Spring 5.0?