【问题标题】:Custom jpa repository method published by spring-data-restspring-data-rest 发布的自定义 jpa 存储库方法
【发布时间】:2014-02-02 16:32:37
【问题描述】:

我已向 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 控制器)?

【问题讨论】:

  • 我也面临同样的问题。任何帮助将不胜感激。

标签: java spring spring-data spring-data-jpa spring-data-rest


【解决方案1】:

我检查了代码库 - 似乎他们明确禁用了自定义方法 - 不知道为什么。这是来自 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);
}

【讨论】:

  • 我想知道是否可以通过猴子补丁绕过此检查,以便它接受自定义方法?
  • 我很想听听 SDR 作者的意见,为什么会这样。或者提及一个 jira 问题以允许这种行为。
  • Oliver Gierke 在这里解释了原因:stackoverflow.com/questions/25201306/…
猜你喜欢
  • 2018-10-06
  • 2016-06-25
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 2012-06-02
  • 2015-08-17
  • 2017-08-20
  • 2015-05-10
相关资源
最近更新 更多