gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:33 25 4
gpt4 key购买 nike

如何覆盖 spring 数据存储库以在转到从 spring data rest 中的/api 页面发现的页面时仅选择选定的列。

我添加了 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 可以在不更改存储库的情况下获得所需的结果。

关于java - 覆盖 Pageable findAll 以在 Spring Data Rest 中选择更少的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26521241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com