Spring Data Rest PagingAndSortingRepository AbstractMethodError (RepositoryFactorySupport)

Posted

技术标签:

【中文标题】Spring Data Rest PagingAndSortingRepository AbstractMethodError (RepositoryFactorySupport)【英文标题】: 【发布时间】:2015-12-01 01:47:06 【问题描述】:

我正在尝试使用 Spring Data Rest 构建一个 Rest Api,在多次更改我的 pom.xml 以找到我的项目的兼容依赖项之后,我现在遇到了这个问题:

java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;

从前...

我有一个 Java Config Web App,我的 JPAConfig 有这些注解

@Configuration
//@EnableJpaRepositories("com.protect.inf.jpa.repositories")
@EnableJpaRepositories
@EnableTransactionManagement
public class JPAConfig  ... 

查看@EnableJpaRepositories 注释,如果我没有设置我的 Repositories 类所在的包名,应用程序启动正常,但是当我向 api 发出请求时,响应如下所示:


_links: 
    profile: 
        href: "http://localhost:8080/protect-inf-01/api/profile"
        -
    -

它不会暴露我的 Repos url。我的 Repos 类看起来像这样

@RestResource(path = "users", rel = "users")
public interface UsuariosRepository extends PagingAndSortingRepository<Usuarios, Integer>
    Usuarios findByEmail(String email);

然后...

当我将包名称设置为 @EnableRepositories 注释 @EnableJpaRepositories("com.protect.inf.jpa.repositories") 时,应用程序启动失败并出现以下错误:

Error creating bean with name 'cuentasRepository': 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;

我认为这是我的 Maven 中的一个依赖问题,我不太确定。 这是我的 pom.xm

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>protect-inf-01</groupId>
    <artifactId>protect-inf-01</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>protect-inf-01 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <jackson.version>1.9.13</jackson.version>
        <hibernate.validator.version>4.2.0.Final</hibernate.validator.version>
        <org.springframework-version>4.1.7.RELEASE</org.springframework-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/libs-milestone/</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
            <version>2.4.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.11.0.RC1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.8.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.6.Final</version>
        </dependency>

        <!-- SERVLET -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>$org.slf4j-version</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>$org.slf4j-version</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>$org.slf4j-version</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>protect-inf-01</finalName>
    </build>
</project>

这是我的WebAppInitializer 课程

package com.protect.inf.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer 

    @Override
    protected Class<?>[] getRootConfigClasses() 
        return new Class<?>[]JPAConfig.class;
    

    @Override
    protected Class<?>[] getServletConfigClasses() 
        return new Class[]WebConfiguration.class;
    

    @Override
    protected String[] getServletMappings() 
        return new String[]"/";
    


这是我的WebConfiguration 课程

package com.protect.inf.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

@Configuration
public class WebConfiguration extends RepositoryRestMvcConfiguration
    @Override
      public RepositoryRestConfiguration config() 
        RepositoryRestConfiguration config = super.config();
        config.setBasePath("/api");
        return config;
      

欢迎任何帮助,谢谢!

已解决 删除spring-data-commons 并将spring-data-jpa 版本更改为1.9.0.RELEASE 就像@peeskillet 所说的现在可以正常工作!

注释 spring-data-commons 依赖

<!--        <dependency> -->
<!--            <groupId>org.springframework.data</groupId> -->
<!--            <artifactId>spring-data-commons</artifactId> -->
<!--            <version>1.11.0.RC1</version> -->
<!--        </dependency> -->

更改 spring-data-jpa 版本

<!--        <dependency> -->
<!--            <groupId>org.springframework.data</groupId> -->
<!--            <artifactId>spring-data-jpa</artifactId> -->
<!--            <version>1.8.2.RELEASE</version> -->
<!--        </dependency> -->

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.0.RELEASE</version>
    </dependency>

谢谢peeskillet!

【问题讨论】:

看看如果你摆脱 spring-data-commons 并将 spring-data-jpa 版本更改为 1.9.0.RELEASE 会发生什么 太棒了! Ha funcionado perfecto!!! 你能解释一下为什么吗? spring-data-jpa 包括 spring-data-commons?谢谢! 【参考方案1】:

去掉spring-data-commons,将spring-data-jpa版本改为1.9.0.RELEASE

spring-data-rest-webmvcspring-data-jpa 都已经依赖于 spring-data-commons。但是您需要确保它们都依赖于相同的版本。 s-d-rest-webmvc-2.4.0 取决于 s-d-commons-1.11.0s-d-jpa-1.8.2 取决于 s-d-commons-1.10.2。这就是区别。 s-d-jpa-1.9.0 依赖于 s-d-commons-1.11.0

【讨论】:

以上是关于Spring Data Rest PagingAndSortingRepository AbstractMethodError (RepositoryFactorySupport)的主要内容,如果未能解决你的问题,请参考以下文章

排除 Spring-data-rest 资源的部分字段

如何在 Spring-Data-Rest 中实现细粒度的访问控制?

初入spring boot(八 )Spring Data REST

Spring-Data-Rest中时间的数据类型

您如何保护 Spring Boot / Spring-Data Rest 以便用户只能访问他自己的实体

spring-data-rest 集成测试因简单的 json 请求而失败