I'm able to "solve" this with the following (ugly and smelly) code:
List<Message> messages = messagesRepository.findAll().get();
return Try.success(
messages
.stream()
.map(m -> new SummarizedMessage(m.destination(), m.title(), m.date()))
.toList()
);
But how can I avoid this get()
and Try.success()
?
You can use map
and orElse
.
messagesRepository.findAll().map(messages -> messages.stream()
.map(m -> new SummarizedMessage(m.destination(), m.title(), m.date()))
.toList()).orElse(null);