findAll 与 CrudRepository 在 Projections 中的 findAll 冲突

findAll clashes with findAll with CrudRepository in Projections

登录

@ApiModel
@Entity
public class Login {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private LocalDateTime loginDateTime;

    /** Other fields ***/
} 

仅登录日期

interface LoginDateOnly {

    @Value("#{target.loginDateTime.toLocalDate()}")
    LocalDate getDateFromLoginDateTime();

}

登录存储库

@RepositoryRestResource(collectionResourceRel = "login", path = "login")
public interface LoginRepository extends PagingAndSortingRepository<Login, Long> {

    Collection<LoginDateOnly> findAll();

    /** Other query methods **/
}

我只是想获取 所有 我的登录记录,以及 loginDateTime selected/projected 的 LocalDate 部分使用 http://host/api/login。但目前我遇到了与 CrudRepository 的 findAll() 的冲突。如何使用投影尽可能地解决这个问题。我将@Query 和@NamedQuery 作为最后的手段。

一个findAll方法签名是:

List<T> findAll();

如果您想覆盖它,则不能使用其他签名。

要获得投影列表,您只需为此定义另一种方法,例如:

Collection<LoginDateOnly> findAllBy();

但正如我所见,您正在使用 Spring Data REST,因此在这种情况下您不需要定义新方法。您应该首先将注释 @Projection 添加到您的投影中:

@Projection(name = "loginDateOnly", types = Login.class)
interface LoginDateOnly {
    //...
}

然后在请求中使用它的名字url:

GET http://host/api/login?projection=loginDateOnly

在文档中查看更多信息:Projections and Excerpts