spring-data-rest发布的自定义jpa存储库方法

And*_*res 10 java spring spring-data spring-data-jpa spring-data-rest

我已经在jpa存储库中添加了一个自定义方法,详见http://docs.spring.io/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-implementations

据我所知,当我使用spring-data-rest时,这个方法不会暴露.有没有什么办法可以将它作为spring-data-rest生成的REST API的一部分发布(不用自己创建Spring MVC Controller)?

Fai*_*roz 11

我检查了代码库 - 似乎他们已经明确禁用自定义方法 - 不知道为什么.以下是org.springframework.data.repository.core.support.DefaultRepositoryInformation的相关代码.

@Override
public Set<Method> getQueryMethods() {

    Set<Method> result = new HashSet<Method>();

    for (Method method : getRepositoryInterface().getMethods()) {
        method = ClassUtils.getMostSpecificMethod(method, getRepositoryInterface());
        if (isQueryMethodCandidate(method)) {
            result.add(method);
        }
    }

    return Collections.unmodifiableSet(result);
}

/**
 * Checks whether the given method is a query method candidate.
 * 
 * @param method
 * @return
 */
private boolean isQueryMethodCandidate(Method method) {
    return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method);
}
Run Code Online (Sandbox Code Playgroud)

  • 我很想听听SDR作者为何会这样做.或者提一个jira问题来允许这种行为. (2认同)
  • Oliver Gierke在此解释了原因:http://stackoverflow.com/questions/25201306/implementing-custom-methods-of-spring-data-repository-and-exposing-them-through (2认同)