mengmeng :
I can't seem to figure out why I'm getting this error on the
IDE Unexpected return value
when I need to return something from a method when using lambda.
public Employee getEmployee(long id) {
repository.findById(id).ifPresentOrElse(
empDetails -> {
return service.buildEmployee(empDetails);
},
() -> { throw new ResourceNotFoundException(); }
);
}
Thank you!
Eran :
ifPresentOrElse
is used to consume the Optional
's value if present, and to perform some other logic otherwise. It cannot be used to return a value or throw an exception.
Instead you can combine map
with orElseThrow
:
public Employee getEmployee(long id) {
return repository.findById(id)
.map(service::buildEmployee)
.orElseThrow(ResourceNotFoundException::new);
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=292379&siteId=1