如何将JpaRepository结果转换为我的自定义DTO类

时间:2019-02-25 04:28:12

标签: java spring spring-boot spring-data-jpa

我想将页面转换为页面

所以我这样实现了

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

可悲。它给了我

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])

出什么问题了......:(

1 个答案:

答案 0 :(得分:1)

您的DTO类需要具有默认的构造函数,getter和setter。尝试使用龙目岛的@Data对其进行注释。