猿问

为什么在 spring boot 中没有收到可分页的详细信息?

我在 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<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));

}

汇编器:


@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();

    }

}

存储库:


@Repository

public interface TopicRepo extends JpaRepository<Topic, UUID> {


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


}


Qyouu
浏览 177回答 2
2回答

沧海一幻觉

将返回类型调整为Page<PostOutputDTO>而不是List<PostOutputDTO>。将 a 转换List<PostOutputDTO>为 a的最简单方法Page<PostOutputDTO>是public Page<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {&nbsp; &nbsp; return new PageImpl<>(postService.getActivePosts(topicId, pageable));}更新:我仍然没有看到全貌,但我希望存储库方法返回Page实例,Page<Post> findPostsByTopicAndDeactivatedAtIsNull(...);Page<Topic> findAllByTypeAndDeactivatedAtIsNull(...);所以问题来自于PostAssembler#toResources返回 a List,我们不准确地将其转换为Pageback 。如果我理解正确,您正在利用ResourceAssemblerSupport将 an Iterable<Post>(更准确地说, a Page<Post>)映射到 a List<PostOutputDTO>。我的建议是不使用PostAssembler#toResources,坚持PostAssembler#toResource和Page#map:postRepo.findPostsByTopicAndDeactivatedAtIsNull(topic, pageable)&nbsp; &nbsp; &nbsp; &nbsp; .map(postAssembler::toResource);

Cats萌萌

您必须从 spring 返回接口页面:页面是对象列表的子列表。它允许获取有关它在包含中的位置的信息。看例子:public Page<PostOutputDTO>&nbsp; getActivePosts(UUID topicId, Pageable pageable) {&nbsp; &nbsp; Page<PostOutputDTO>&nbsp; list=postService.getActivePosts(topicId, pageable);&nbsp; &nbsp; &nbsp; return&nbsp; list;}有关更多信息,请参阅参考
随时随地看视频慕课网APP

相关分类

Java
我要回答