我在 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);
}
沧海一幻觉
Cats萌萌
相关分类