Spring data Rest - 公开默认方法

rat*_*mar 5 spring-data-rest default-method spring-repositories

我有一个人员存储库,如下所示

@RepositoryRestResource
public interface PersonRepository extends Repository<Person, String> {
    List<Person> findAll();

    default List<Person> findNewPersons() {
         return findByStartDateAfter(LocalDate.now().minusMonths(3));
    }
    List<Person> findByStartDateAfter(LocalDate date);
}
Run Code Online (Sandbox Code Playgroud)

我无法通过休息公开默认方法。有没有办法在不创建存储库实现的情况下做到这一点?

Pla*_*One 2

我遇到了类似的问题,并且能够在注释中的 HQL 查询中使用 SpEL 表达式来解决它@Query

虽然远不如使用默认方法那么干净,但这是我能找到的最简洁的方法,无需编写自定义控制器或使用新的 DSL 库引入自定义实现或仅针对此一个查询。

@Query("select p from Person p where p.startDate > :#{#T(java.time.LocalDate).now().minusMonths(3)}")
List<Person> findNewPersons();
Run Code Online (Sandbox Code Playgroud)

我的实际查询是不同的,所以我可能在这里拼写了语法,但想法是相同的,并且它适用于我的情况(我使用参数LocalDate并通过使用样式查询查找当天的时间戳findByTimestampBetween)。