Attempting to execute multiple commands and return an Object (ResponseEntity) using Optional.ifPresentOrElse
More complex does not work:
return ifPresentOrElse(
repoService.findById(inId),
i -> {
i.setStatus(inStatus);
repoService.save(i);
ResponseEntity.ok().body(i);
},
() -> {
LOG.error("Object not available");
ResponseEntity.notFound().build();
});
Simpler works but doesn't execute all the commands required:
Optional<I> iOptional = repoService.findById(inId);
return iOptional.map(i -> ResponseEntity.ok().body(i))
.orElse(ResponseEntity.notFound().build());
Is there away to achieve this using Optional functions?