【问题标题】:Spring Data Rest and spring-data-rest-hal-browser integration using Spring BootSpring Data Rest 和 spring-data-rest-hal-browser 使用 Spring Boot 集成
【发布时间】:2015-11-09 15:01:20
【问题描述】:

问题

Spring Data REST Reference Documentation 这么说

dependencies {
    compile 'org.springframework.data:spring-data-rest-hal-browser'
}

足以在使用 Spring Boot 时将 HAL 浏览器与 Spring Data Rest 集成,但 Gradle 抱怨无法找到此依赖项,除非我指定特定版本,因此我通过指定 the latest one available in central repository 来修复它(存储库中没有其他版本可用) .指定版本后,依赖项已解决,但在 Spring Boot 容器初始化期间出现错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cameraRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 151 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cameraRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:966)
    at org.springframework.data.repository.support.Repositories.cacheRepositoryFactory(Repositories.java:95)
    at org.springframework.data.repository.support.Repositories.populateRepositoryFactoryInformation(Repositories.java:88)
    at org.springframework.data.repository.support.Repositories.<init>(Repositories.java:81)
    at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.repositories(RepositoryRestMvcConfiguration.java:182)
    at org.springframework.boot.autoconfigure.data.rest.SpringBootRepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$b6ea42c0.CGLIB$repositories$11(<generated>)
    at org.springframework.boot.autoconfigure.data.rest.SpringBootRepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$b6ea42c0$$FastClassBySpringCGLIB$$ec6a4119.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at org.springframework.boot.autoconfigure.data.rest.SpringBootRepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$b6ea42c0.repositories(<generated>)
    at sun.reflect.NativeMetssorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 152 more
Caused by: java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
    ... 173 more

但是,如果我删除 spring-data-rest-hal-browser:2.4.0.RELEASE 依赖项,我的应用程序会成功初始化并按预期工作。

问题

如何正确设置我的build.gradle 以使我的应用程序与 HAL 浏览器正确集成?


build.gradle

buildscript {
    ext {
        springBootVersion = '1.2.7.RELEASE'
    }

    repositories {
        mavenCentral()
    }
}

dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    classpath('io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE')
}

[...]

repositories {
    mavenCentral()
    flatDir {
       dirs 'lib'
    }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-remote-shell')
    compile('org.springframework.data:spring-data-rest-hal-browser:2.4.0.RELEASE')
    compile('org.projectlombok:lombok:1.16.6')
    compile name: 'ojdbc6'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc:1.0.0.RELEASE')
}

【问题讨论】:

  • 您确定您提供了正确的脚本吗?根据它,您在 buildscript 配置闭包中拥有所有配置,但某些配置必须位于脚本的根目录中。
  • 是的,你是对的。我搞砸了一些花括号和缩进。我已经更正了。

标签: spring gradle spring-boot build.gradle spring-data-rest


【解决方案1】:

HAL 浏览器的自动版本管理仅从 Spring Boot 1.3.0 开始工作,该版本处于候选发布阶段,应该很快就会发布。候选版本对我来说非常稳定,因此您可以考虑通过将构建更新到 1.3.0.RC1 来尝试一下,我希望这会解决问题,如下所示:

buildscript {
    ext {
        springBootVersion = '1.3.0.RC1'
    }
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone/' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
    }
}

repositories {
    mavenCentral()
    flatDir {
       dirs 'lib'
    }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-remote-shell')
    compile('org.springframework.data:spring-data-rest-hal-browser')
    compile('org.projectlombok:lombok:1.16.6')
    compile name: 'ojdbc6'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

您可以在此处查看 1.3.0.RC1 的托管依赖版本:

http://docs.spring.io/spring-boot/docs/1.3.0.RC1/reference/htmlsingle/#appendix-dependency-versions

如果您查看 1.2.7 的托管依赖版本,您会注意到 spring-data-rest-hal-browserspring-restdocs-mockmvc 都不存在。

【讨论】:

    【解决方案2】:

    包已重命名为spring-data-rest-hal-explorer。下面是一个使用 Spring Boot 2 和自动版本管理的 HAL 的现代配置示例。

    Maven 示例:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-hal-explorer</artifactId>
    </dependency>
    

    Gradle 示例:

    plugins {
        id "io.spring.dependency-management" version <<version>>
    }
    
    dependencies {
        implementation('org.springframework.data:spring-data-rest-hal-explorer')
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2014-07-05
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2014-11-17
      相关资源
      最近更新 更多