Java 为什么不在spring boot中接收可分页的详细信息?

Java 为什么不在spring boot中接收可分页的详细信息?,java,spring,spring-boot,Java,Spring,Spring Boot,我在一个spring boot项目中工作。我需要返回带有页码的列表。目前,我可以在参数中选择页面和大小,但我没有收到如下页面详细信息: "last": false, "totalElements": 20, "totalPages": 7, "size": 3, "number": 0, "sort": null, "first": true, "numberOfElements": 3 我只是收到了一个正常的回复,没有这个。我认为我需要将方法响应类型更改为ResponseEntity或Res

我在一个spring boot项目中工作。我需要返回带有页码的列表。目前,我可以在参数中选择页面和大小,但我没有收到如下页面详细信息:

"last": false,
"totalElements": 20,
"totalPages": 7,
"size": 3,
"number": 0,
"sort": null,
"first": true,
"numberOfElements": 3
我只是收到了一个正常的回复,没有这个。我认为我需要将方法响应类型更改为ResponseEntity或Resources。 有什么想法吗

控制器:

public List<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {
   return postService.getActivePosts(topicId, pageable);
public List getTopicPosts(@PathVariable(“id”)UUID-topicId,Pageable-Pageable){
return postService.getActivePosts(topicId,可分页);
服务:

 public List<PostOutputDTO> getActivePosts(UUID topicId, Pageable pageable) throws AccessDeniedException {
    Topic topic = topicRepo.findByIdAndDeactivatedAtIsNull(topicId).orElseThrow(() -> new EntityNotFoundException("This topic doesn't exist."));
    if (topic.getType() != TopicType.GLOBAL) {
        throw new AccessDeniedException("This is not a global topic.");
    }
    return postAssembler.toResources(postRepo.findPostsByTopicAndDeactivatedAtIsNull(topic, pageable));
}
public List getActivePosts(UUID-topicId,Pageable-Pageable)抛出AccessDeniedException{
Topic Topic=topicRepo.findbyidandeactivatedatisnull(topicId.orelsetrow(()->newentitynotfoundexception(“此主题不存在”);
if(topic.getType()!=TopicType.GLOBAL){
抛出新的AccessDeniedException(“这不是一个全局主题”);
}
返回postmassembler.toResources(postRepo.findpostsbytopican和deactivatedatisnull(主题,可分页));
}
汇编程序:

@Service
public class PostAssembler extends ResourceAssemblerSupport<Post, PostOutputDTO> {

    @Autowired
    private ForumUserAssembler forumUserAssembler;

    @Autowired
    private TopicAssembler topicAssembler;

    @Autowired
    private ContentRepo contentRepo;

    public PostAssembler() {
        super(PostController.class, PostOutputDTO.class);
    }

    public PostOutputDTO toResource(Post post) {
        return PostOutputDTO.builder()
                .uuid(post.getId())
                .topic(topicAssembler.toResource(post.getTopic()))
                .text(contentRepo.findByPostAndDeactivatedAtIsNull(post).orElseThrow(() -> new EntityNotFoundException("This post doesn’t have content")).getText())
                .createdAt(post.getCreatedAt())
                .createdBy(forumUserAssembler.toResource(post.getCreatedBy()))
                .build();
    }
}
@服务
公共类PostAssembler扩展了ResourceAssemblerSupport{
@自动连线
专用ForumUserAssembler ForumUserAssembler;
@自动连线
私人话题组装商话题组装商;
@自动连线
私人ContentRepo ContentRepo;
公共邮政组装商(){
super(PostController.class、PostOutputDTO.class);
}
公共Post输出到存储资源(Post){
返回PostOutputDTO.builder()
.uuid(post.getId())
.topic(topicAssembler.toResource(post.getTopic()))
.text(contentRepo.findByPostAndDeactivatedAtIsNull(post).orelsetrow(()->new EntityNotFoundException(“此帖子没有内容”)).getText()
.createdAt(post.getCreatedAt())
.createdBy(forumUserAssembler.toResource(post.getCreatedBy()))
.build();
}
}
存储库:

@Repository
public interface TopicRepo extends JpaRepository<Topic, UUID> {

    Page<Topic> findAllByTypeAndDeactivatedAtIsNull(TopicType topicType, Pageable pageable);

}
@存储库
公共接口TopicRepo扩展了JpaRepository{
Page FindAllByType和DeactivatedAtisNull(TopicType TopicType,可分页);
}
将返回类型调整为而不是
列表

列表
转换为
页面
的最简单方法是

public Page<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {
    return new PageImpl<>(postService.getActivePosts(topicId, pageable));
}
因此问题来自于
postmassembler#toResources
,它返回一个
列表
,我们错误地将其转换为
页面

如果我没有弄错的话,您正在使用
ResourceAssemblerSupport
Iterable
(更准确地说,是
页面
)映射到
列表

我的建议是不要使用和坚持:

将返回类型调整为而不是
列表

列表
转换为
页面
的最简单方法是

public Page<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {
    return new PageImpl<>(postService.getActivePosts(topicId, pageable));
}
因此问题来自于
postmassembler#toResources
,它返回一个
列表
,我们错误地将其转换为
页面

如果我没有弄错的话,您正在使用
ResourceAssemblerSupport
Iterable
(更准确地说,是
页面
)映射到
列表

我的建议是不要使用和坚持:


您必须从spring返回接口页:

页面是对象列表的子列表。它允许获取有关其在包含对象列表中的位置的信息

请参见示例:

public Page<PostOutputDTO>  getActivePosts(UUID topicId, Pageable pageable) {

    Page<PostOutputDTO>  list=postService.getActivePosts(topicId, pageable);

      return  list;

}
公共页面getActivePosts(UUID-topicId,Pageable-Pageable){
Page list=postService.getActivePosts(topicId,可分页);
退货清单;
}

有关更多信息,请参见spring的“您必须返回界面”页面:

页面是对象列表的子列表。它允许获取有关其在包含对象列表中的位置的信息

请参见示例:

public Page<PostOutputDTO>  getActivePosts(UUID topicId, Pageable pageable) {

    Page<PostOutputDTO>  list=postService.getActivePosts(topicId, pageable);

      return  list;

}
公共页面getActivePosts(UUID-topicId,Pageable-Pageable){
Page list=postService.getActivePosts(topicId,可分页);
退货清单;
}

欲了解更多信息,请参阅

工作!谢谢!@Chefk5很乐意为您提供帮助您知道为什么使用“totalElements”不显示所有元素。它仅在当前page@Chefk5请看这里:@Chefk5您可以不使用
PageImpl
,而是直接从repositoryWorked返回
Page
!谢谢!@Chefk5很乐意帮忙您知道为什么使用“totalElements”吗不显示所有元素。它仅在当前page@Chefk5请看这里:@Chefk5您可以不使用
PageImpl
,而是直接从存储库返回
页面
,这是一个接口页面如果postService返回页面,您可以返回它,这与return postService.getActivePosts(topicId,可分页)相同;您不需要返回实现new PageImpl,因为存储库必须返回如下可分页:公共接口示例存储库扩展分页和排序存储库{public Page findAllExampleObjects(可分页);}好的..现在我在汇编程序的最后一行服务中遇到了一个问题…尽管我将返回类型更改为pageits是一个接口页面,如果postService返回一个页面,您可以返回它,它与return postService.getActivePosts(topicId,pageable)相同;您不需要返回实现new PageImpl,因为存储库必须返回如下可分页:公共接口示例存储库扩展分页和排序存储库{public Page findAllExampleObjects(可分页);}好的。现在我在汇编程序的最后一行服务中遇到了一个问题…尽管我将返回类型更改为page