Spring Data REST - 覆盖存储库 findAll 而不创建/search/findAll URL [英] Spring Data REST - Override repository findAll without creating /search/findAll URL

查看:37
本文介绍了Spring Data REST - 覆盖存储库 findAll 而不创建/search/findAll URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以阻止 Spring Data REST 为重写的存储库方法创建/search URL?

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

例如,以下代码会生成一个/search/findAll URL,该 URL 复制了集合资源的功能:

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();
}

这只是覆盖单个方法时的表面问题,但如果您尝试覆盖具有相同函数名称和不同参数的多个方法,例如 PagingAndSortingRepository 中的两个 findAll 方法,则 spring 会抛出异常,因为它试图映射 2 个函数到相同的路径.

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);
}

结果:

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)
    ...

推荐答案

有什么办法可以阻止 Spring Data REST 为重写的存储库方法创建/search URL?

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);
}

享受吧!))

@RestResource(exported=false) 仅用于覆盖的方法将无济于事,因为这会阻止 GET '所有记录' 请求 (

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

这篇关于Spring Data REST - 覆盖存储库 findAll 而不创建/search/findAll URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆