10

I've added a custom method to a jpa repository as detailed on http://docs.spring.io/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-implementations

As far as I could see, this method is not exposed when I use spring-data-rest. Is there any way I could publish it as part of the REST API generated by spring-data-rest (without creating a Spring MVC Controller myself)?

Andres
  • 10,561
  • 4
  • 45
  • 63

1 Answers1

11

I checked the code base - seems like they have explicitily disabled custom methods - not sure why. Here is the relevant piece of code from 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);
}
Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
  • 1
    I wonder if one could monkey-patch to bypass this check so that it accepts custom methods? – wakandan Jun 03 '14 at 02:44
  • 2
    I would love to hear from the SDR authors as to why this is. Or mention a jira issue to allow this behavior. – Jason May 03 '15 at 23:55
  • 2
    Oliver Gierke explained why here : http://stackoverflow.com/questions/25201306/implementing-custom-methods-of-spring-data-repository-and-exposing-them-through – JR Utily Oct 28 '15 at 16:51