编程语言
首页 > 编程语言> > java-覆盖Pageable findAll以在Spring Data Rest中选择较少的列

java-覆盖Pageable findAll以在Spring Data Rest中选择较少的列

作者:互联网

当转到从Spring数据库中的/ api页面发现的页面时,如何覆盖spring数据存储库以仅选择选定的列.

我添加了findAll如下-

public interface UserRepository extends BaseRepository<User, Integer>, UserRepositoryCustom {

    @Query("select u from User u where email = :email and password = :password")
    @Cacheable(value = "user-cache", key = "#user.login")
    @RestResource(exported = false)
    public User findUserByEmailAndPassword(@Param("email") String email, @Param("password") String password);

    @RestResource(rel = "byEmail", path = "byEmail")
    public User findUserByEmail(@Param("email") String email);

    @RestResource(rel = "byPhone", path = "byPhone")
    public User findUserByPhone(@Param("phone") String phone);

     @Override 
     @Query("select u.id,u.email,u.phone from User u ")
     public Page<User> findAll(Pageable pageable);
}

/ api / users提供了一个错误-

{"cause":null,"message":"PersistentEntity must not be null!"}

解决方法:

我在与User相同的包中创建了UserSummaryProjection类

@Projection(name = "summary", types = User.class)
public interface UserSummaryProjection {

    Integer getId();

    String getEmail();

}

然后,转到/ api / users或/ users / 3?projection = summary即可得到所需的结果,而无需更改存储库.

标签:spring-data-rest,spring-data,spring,java
来源: https://codeday.me/bug/20191121/2049415.html