How to Override spring data repository to select only selected columns when going to pages that are discovered from /api page in spring data rest.
I added findAll as 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 is giving an error -
{"cause":null,"message":"PersistentEntity must not be null!"}
I created a UserSummaryProjection
class in same package as User
@Projection(name = "summary", types = User.class)
public interface UserSummaryProjection {
Integer getId();
String getEmail();
}
Then, going at /api/users
or /users/3?projection=summary
gives me desired result without changing the Repository.
Selecting subelements of User and still creating a User is somewhat counterintuitive.
I would create another entity for example UserDetails , that will be mapped by the same 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 Entity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.