spring-projects / spring-projects/spring-data-rest
Expose Custom repository via REST [DATAREST-845]
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
urbanq opened DATAREST-845 and commented
it would be nice to have a possibility to expose methods from Custom repositoy via REST.
I found a workaround, that might be helpful:
// extend org.springframework.data.repository.core.support.DefaultRepositoryInformation
class DefaultRepositoryInformation implements RepositoryInformation {
...
public Set<Method> getCustomQueryMethods() {
Set<Method> result = new HashSet<>( );
for (Method method : getRepositoryInterface().getMethods()) {
method = ClassUtils.getMostSpecificMethod(method, getRepositoryInterface());
if (isCustomMethod(method)) {
result.add(method);
}
}
return Collections.unmodifiableSet(result);
}
...
}
and:
// extend org.springframework.data.rest.core.mapping.RepositoryResourceMappings
public class RepositoryResourceMappings extends PersistentEntitiesResourceMappings {
...
@Override
public SearchResourceMappings getSearchResourceMappings( Class<?> domainType ) {
Assert.notNull(domainType, "Type must not be null!");
if (searchCache.containsKey(domainType)) {
return searchCache.get(domainType);
}
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainType);
List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>();
ResourceMetadata resourceMapping = getMetadataFor(domainType);
if (resourceMapping.isExported()) {
for (Method queryMethod : repositoryInformation.getQueryMethods()) {
RepositoryMethodResourceMapping methodMapping = new RepositoryMethodResourceMapping(queryMethod,
resourceMapping, repositoryInformation);
if (methodMapping.isExported()) {
mappings.add(methodMapping);
}
}
//////////////////////////////////
// method BODY EXTENSION STARTS //
for ( Method queryMethod : repositoryInformation.getCustomQueryMethods( ) ) {
RepositoryMethodResourceMapping methodMapping = new RepositoryMethodResourceMapping( queryMethod,
resourceMapping, repositoryInformation );
if ( methodMapping.isExported( ) ) {
mappings.add( methodMapping );
}
}
// method BODY EXTENSION ENDS //
//////////////////////////////////
}
SearchResourceMappings searchResourceMappings = new SearchResourceMappings(mappings);
searchCache.put(domainType, searchResourceMappings);
return searchResourceMappings;
}
...
}
Reference URL: http://stackoverflow.com/questions/21116539/custom-jpa-repository-method-published-by-spring-data-rest
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading DefaultRepositoryInformation and RepositoryResourceMappings, especially getCustomQueryMethods and getSearchResourceMappings, along with the provided workaround. Determine how custom repository methods should be included in REST search mappings, then verify that exposed custom methods are available through the REST API and do not disrupt existing query mappings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100