Override Pageable findAll to select fewer columns in Spring Data Rest

How to override spring data repository to select only selected columns when navigating to pages found from / api pages in spring data.

I added findAll as shown below -

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 report an error -

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

      

+3


source to share


2 answers


I created a class UserSummaryProjection

in the same package asUser

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

    Integer getId();

    String getEmail();

}

      



Then switching to /api/users

or /users/3?projection=summary

gives the desired result without changing the repository.

+3


source


The choice of user subelements and user creation are somewhat controversial.

I would create another object, like UserDetails , which will be displayed by a single table with the same mapping.



public class UserDetails {
    private int uid;
    private String email;
    private String phone;
}

      

And create a repository based on this new object.

0


source







All Articles