【问题标题】:Spring Boot JPA H2 Console not running, application.properties file ignoredSpring Boot JPA H2 控制台未运行,application.properties 文件被忽略
【发布时间】:2017-03-08 18:17:28
【问题描述】:

Spring Boot 指南说我可以获得 H2 控制台,但它不适合我。

http://localhost:8080/h2/ Whitelabel 错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。 2016 年 10 月 26 日星期三 12:31:46 BST 出现意外错误(类型=未找到,状态=404)。 没有可用的消息

我创建了一个application.properties 文件如下

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

我的项目基于this

默认路径/h2-console 也不起作用。

我找到了另一个答案,通过添加Application.java

    @Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/h2/*");
    return registration;
}

我的application.properties 文件中的所有内容都将被忽略。我已经尝试添加:

spring.datasource.url=jdbc:h2:file:~/portal;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

但数据库仍然只在内存中创建。

【问题讨论】:

  • 您的application.properties 在哪里以及您使用的是哪个版本的 Spring Boot。
  • javaworkspace\gs-accessing-data-rest-initial\src\main\java\hello\application.propertiesorg.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE
  • 那是错误的...它是一个非 java 源代码,所以它会被忽略它应该放在src\main\resources...旁边它必须在 te 根而不是子包中。

标签: gradle spring-boot h2


【解决方案1】:

检查是否在 application.properties 中设置了基本路径。

例如,如果你有一个设置

server.contextPath=/api

您在

下访问 h2 控制台

http://localhost:8080/api/h2-console

很明显,但对我来说就是这样

【讨论】:

  • 谢谢!这是我的情况!
【解决方案2】:

您当前的位置src\main\java\h‌​ello\application.pro‌​perties 是罪魁祸首。有两个原因。

  1. src\main\java 中的非 java 资源被忽略
  2. 仅在根目录或config 目录中的application.properties 或考虑(默认情况下)。 (见reference guide)。

解决方法是将您的application.properties 移动到src\main\resources

【讨论】:

    【解决方案3】:

    /spring.h2.console.path 之前丢失,它必须看起来像:

    spring.h2.console.path=/h2
    

    当您指出spring.h2.console.path /h2-console 不再可用时

    问候

    【讨论】:

    • 谢谢,我尝试更改它,但没有帮助,请参阅编辑。
    • 您有足够的权限在 ~/portal 上写作吗?您是否尝试过完整路径而不是 ~ ?任何堆栈跟踪?尝试但 logger=debug
    • 如果我使用 H2 控制台访问 ~/portal 那么它将在我的主目录中创建 .mv 数据库文件,但它们当然是空白的。由控制台而不是我的应用程序创建。它们都以相同的权限级别运行。
    • 也许你可以在resources文件夹中添加.h2.server.properties,内容如下:#H2 Server Properties 0=JHipster H2 (Disk)|org.h2.Driver|jdbc\:h2\:file \:./target/h2db/db/product|产品 webAllowOthers=true webPort=8082 webSSL=false
    【解决方案4】:

    此问题的另一个可能原因是您使用的是 spring security。 在这种情况下,您可能希望向您定义的 h2-console URL 添加特定权限。 例如,对于默认的 h2-console 配置(没有 spring.h2.console.path 属性),您将在 WebSecurityConfigurerAdapter 扩展类中添加它:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/h2-console/**").permitAll()
            .anyRequest().authenticated();
        http.headers().frameOptions().sameOrigin();
    }
    

    还请注意末尾的那一行 - http.headers().frameOptions().sameOrigin();。 登录控制台时需要防止出现 Whitelable 页面错误。 这也被描述为here

    【讨论】:

      【解决方案5】:

      就我而言,我只需要从 h2 依赖项中删除标签 <scope>runtime</scope>,如 here 中所述。

      【讨论】:

        【解决方案6】:

        我在构建我的微服务项目时尝试了 h2-console,但遇到了同样的问题“Whitelabel error page”。 我现在还没有在我的项目中添加任何安全性,但我发现我的问题是通过从 application.properties 中删除 spring.h2.console.enabled= true 并添加依赖项 dev 来解决的-工具。对我来说,不添加开发工具也会导致问题,因此也尝试添加此依赖项。当然,您将添加 h2、执行器、Web 依赖项。

        【讨论】:

          【解决方案7】:

          对我来说 - 计算机重启修复了它。

          不知道为什么会是这个原因,但可能是某个端口被占用,或者 h2 相关文件没有正确部署

          确保当 springBoot 应用程序启动时,您可以看到 Hibernate 和 H2 的日志行:

          2017-04-22 14:41:03.195  INFO 912 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
          2017-04-22 14:41:03.197  INFO 912 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
          2017-04-22 14:41:03.199  INFO 912 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
          2017-04-22 14:41:03.278  INFO 912 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
          2017-04-22 14:41:03.469  INFO 912 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
          2017-04-22 14:41:03.935  INFO 912 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
          2017-04-22 14:41:03.945  INFO 912 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
          

          【讨论】:

            【解决方案8】:

            对于这个问题,我只需在我的 application.properties 中添加默认字符串即可。

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

            也许弹簧靴由于某种原因没有设置这个。

            【讨论】:

              【解决方案9】:

              h2 dependency 中的pom.xml 中删除<scope>test</scope> 在我的情况下有效。 修改后不要忘记重新安装和构建。

              <!-- H2 database -->
              <dependency>
                 <groupId>com.h2database</groupId>
                 <artifactId>h2</artifactId>
                 <version>1.4.199</version>
                 <!-- <scope>test</scope> -->
              </dependency>
              

              【讨论】:

                【解决方案10】:

                我在 Gradle 声明中使用了 compile 否则 testCompile 对我来说效果很好。

                例子:

                compile group: 'com.h2database', name: 'h2', version: '1.4.200'
                

                【讨论】:

                  【解决方案11】:

                  如果您在使用忽略 application.properties 的程序时遇到问题并且您正在使用 Eclipse,请执行以下操作:

                  将application.properties移动到src/main/resources文件夹,右键点击“构建路径”->“添加到构建路径”

                  【讨论】:

                    【解决方案12】:

                    确保使用>spring.datasource.url=jdbc:h2:mem:testdb,因为 h2 需要此数据库

                    【讨论】:

                      【解决方案13】:

                      尝试添加到 application.properties

                      spring.datasource.url=jdbc:h2:mem:testdb
                      spring.datasource.driverClassName=org.h2.Driver
                      spring.datasource.username=sa
                      spring.datasource.password=
                      spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
                      

                      //那么 创建类 SecurityConfig

                      @Configuration
                      @EnableWebSecurity
                      
                      public class SecurityConfig extends WebSecurityConfigurerAdapter {
                      
                      @Override
                      protected void configure(HttpSecurity httpSecurity) throws Exception {
                          httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
                                  .authorizeRequests().antMatchers("/h2-console/**").permitAll();
                          httpSecurity.csrf().disable();
                          httpSecurity.headers().frameOptions().disable();
                      }
                      

                      }

                      【讨论】:

                        【解决方案14】:

                        对我来说 scope=test 是我不小心从 https://mvnrepository.com/artifact/com.h2database/h2/1.4.200 复制的问题

                            <dependency>
                                <groupId>com.h2database</groupId>
                                <artifactId>h2</artifactId>
                                <version>1.4.200</version>
                                <scope>test</scope>
                            </dependency>
                        

                        并替换为

                            <dependency>
                                <groupId>com.h2database</groupId>
                                <artifactId>h2</artifactId>
                                <version>1.4.200</version>
                                <scope>runtime</scope>
                            </dependency>
                        

                        在此之后,它应该使用默认配置。

                        我没有使用 Spring Security,但您可能需要相应地进行某些更改。请检查上面的答案https://stackoverflow.com/a/59729714/7359806

                        【讨论】:

                          【解决方案15】:

                          在 application.properties 中尝试以下操作

                          spring.h2.console.enabled: true
                          

                          【讨论】:

                          • 我不知道为什么这条评论需要 -2 票,但这解决了我的问题。谢谢!
                          猜你喜欢
                          • 1970-01-01
                          • 2016-08-19
                          • 2014-10-06
                          • 2018-04-11
                          • 2014-03-10
                          • 2017-12-25
                          • 1970-01-01
                          • 2014-08-30
                          • 2018-07-12
                          相关资源
                          最近更新 更多