Java 如何将JpaRepository结果转换为自定义DTO类
我想一页一页地转换 所以我就这样实现了Java 如何将JpaRepository结果转换为自定义DTO类,java,json,spring,spring-boot,java-8,Java,Json,Spring,Spring Boot,Java 8,我想一页一页地转换 所以我就这样实现了 @GetMapping("/history") public Page<ResponseAllHistoryDto> getAllHistory() { Pageable pageable = PageRequest.of(0, 20, Sort.by("createdAt")); Page<History> histories = historyRepository.findAll(pageable); r
@GetMapping("/history")
public Page<ResponseAllHistoryDto> getAllHistory() {
Pageable pageable = PageRequest.of(0, 20, Sort.by("createdAt"));
Page<History> histories = historyRepository.findAll(pageable);
return histories.map(history -> {
return ResponseAllHistoryDto.builder()
.history(history)
.tags(tagRepository.findByHistoryHistoryId(history.getHistoryId()))
.auths(authRepository.findByHistoryHistoryId(history.getHistoryId()))
.build();
});
}
问题是什么……(您的
DTO
类需要一个默认构造函数、getter和setter。请尝试使用lombok的@Data
对其进行注释
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.wrapsody.demo.ResponseAllHistoryDto and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0])