spring-boot - 让stream.map返回List Spring boot?
问题描述
尝试将 Author 转换为 Author Dto 以返回 List,但似乎唯一可行的方法是我将 Any 或将其作为 MutableList。有没有什么办法解决这一问题。我将 CrudRepository 用于我的 Author Repo
fun entityToDto(author: Author): AuthorDto? {
return AuthorDto(
id = author.id,
firstName = author.firstName,
lastName = author.lastName,
age = author.age,
publisherName = author.publisher.name
)
}
@GetMapping("/author")
fun getAllAuthors(): Any {
val authorList : List<Author> = authorRepo.findAll().toList()
return authorList.stream().map { x -> entityToDto(x) }.collect(Collectors.toList())
}