java如何将JpaRepository结果转换为自定义DTO类
我想一页一页地转换
所以我就这样实现了
@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 楼答案
类需要有一个默认构造函数、getter和setter。试着用lombok的
@Data
注释它