Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JpaRepository结果转换为自定义DTO类_Java_Json_Spring_Spring Boot_Java 8 - Fatal编程技术网

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