第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在 spring boot 中沒有收到可分頁的詳細(xì)信息?

為什么在 spring boot 中沒有收到可分頁的詳細(xì)信息?

Qyouu 2021-10-27 10:17:43
我在 Spring Boot 項目中工作。我需要返回一個帶分頁的列表。目前我可以在參數(shù)中選擇頁面和大小,但我沒有收到頁面詳細(xì)信息,例如:"last": false,"totalElements": 20,"totalPages": 7,"size": 3,"number": 0,"sort": null,"first": true,"numberOfElements": 3我只是收到?jīng)]有那個的正?;貜?fù)。我想我需要將方法響應(yīng)類型更改為 ResponseEntity 或 Resources。有任何想法嗎?控制器:public List<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {   return postService.getActivePosts(topicId, pageable);服務(wù): 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));}匯編器:@Servicepublic 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();    }}存儲庫:@Repositorypublic interface TopicRepo extends JpaRepository<Topic, UUID> {    Page<Topic> findAllByTypeAndDeactivatedAtIsNull(TopicType topicType, Pageable pageable);}
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗 獲得超5個贊

將返回類型調(diào)整為Page<PostOutputDTO>而不是List<PostOutputDTO>。


將 a 轉(zhuǎn)換List<PostOutputDTO>為 a的最簡單方法Page<PostOutputDTO>是


public Page<PostOutputDTO> getTopicPosts(@PathVariable("id") UUID topicId, Pageable pageable) {

    return new PageImpl<>(postService.getActivePosts(topicId, pageable));

}

更新:


我仍然沒有看到全貌,但我希望存儲庫方法返回Page實例,


Page<Post> findPostsByTopicAndDeactivatedAtIsNull(...);

Page<Topic> findAllByTypeAndDeactivatedAtIsNull(...);

所以問題來自于PostAssembler#toResources返回 a List,我們不準(zhǔn)確地將其轉(zhuǎn)換為Pageback 。


如果我理解正確,您正在利用ResourceAssemblerSupport將 an Iterable<Post>(更準(zhǔn)確地說, a Page<Post>)映射到 a List<PostOutputDTO>。


我的建議是不使用PostAssembler#toResources,堅持PostAssembler#toResource和Page#map:


postRepo.findPostsByTopicAndDeactivatedAtIsNull(topic, pageable)

        .map(postAssembler::toResource);


查看完整回答
反對 回復(fù) 2021-10-27
?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗 獲得超9個贊

您必須從 spring 返回接口頁面:


頁面是對象列表的子列表。它允許獲取有關(guān)它在包含中的位置的信息。


看例子:


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


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


      return  list;


}

有關(guān)更多信息,請參閱參考


查看完整回答
反對 回復(fù) 2021-10-27
  • 2 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號