Monday, May 20, 2019

Java Optional - Good Practice



  • orElseThrow: if personService.getAll() returns an Optional, then:
    return personService.getAll().map(converter::toResource).orElseThrow(
           () -> new NoResourceFound(...)
    );
  • orElse: if personService.getByLastName() returns an optional:
    return personService.getByLastName(lName).orElse(null);

No comments: