简体   繁体   中英

Spring Data REST - Override repository findAll without creating /search/findAll URL

Is there any way to prevent Spring Data REST from creating a /search URLs for overridden repository methods?

For example the following code results in a /search/findAll URL being generated which duplicates the functionality of the collection resource:

public interface EmployeeRepository extends CrudRepository<Employee, Long>
{
    @Override
    @Query("SELECT e FROM Empolyee e")
    Iterable<Employee> findAll();
}

This is only a cosmetic issue when overriding a single method but if you attempt to override multiple methods with the same function name and different parameters, for example both findAll methods in PagingAndSortingRepository then spring throws an exception because it's attempting to map 2 functions to the same path.

public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long>
{
    @Override
    @Query("SELECT e FROM Employee e")
    Iterable<Employee> findAll();

    @Override
    @Query("SELECT e FROM Employee e")
    Iterable<Employee> findAll(Sort sort);

    @Override
    @Query("SELECT e FROM Employee e")  
    Page<Employee> findAll(Pageable pageable);
}

Results in:

java.lang.IllegalStateException: Ambiguous search mapping detected. Both public abstract java.lang.Iterable uk.co.essl.roster.entity.employee.EmployeeRepository.findAll(org.springframework.data.domain.Sort) and public abstract java.lang.Iterable uk.co.essl.roster.entity.employee.EmployeeRepository.findAll() are mapped to /findAll! Tweak configuration to get to unambiguous paths!
    at org.springframework.data.rest.core.mapping.SearchResourceMappings.<init>(SearchResourceMappings.java:60)
    at org.springframework.data.rest.core.mapping.RepositoryResourceMappings.getSearchResourceMappings(RepositoryResourceMappings.java:128)
    at springfox.documentation.spring.data.rest.EntityContext.searchMappings(EntityContext.java:107)
    ...

Is there any way to prevent Spring Data REST from creating a /search URLs for overridden repository methods?

I found following trick to solve this issue:

@Override
default Page<Employee> findAll(Pageable pageable) {
    return findBy(pageable);
}

@RestResource(exported = false)
Page<Employee> findBy(Pageable pageable);

More other this trick allows you to set default sort order for 'get all records' request:

@Override
default Page<Employee> findAll(Pageable p) {
    if (p.getSort() == null) {      
        // The default sort order
        return findBy(new PageRequest(p.getPageNumber(), p.getPageSize(), Sort.Direction.DESC, "myField"));
    }
    return findBy(pageable);
}

Enjoy! ))


@RestResource(exported=false) just for overridden method will not help 'cause this blocks GET 'all records' request (

@RestResource(exported = false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM