Sunday, March 29, 2020

java.util.Optional means getOrElseThrowNoSuchElementException

java.util.Optional 
...means getOrElseThrowNoSuchElementException

An example:

    @Autowired
    private SimulationDao simulationDao;

@GetMapping(value = "/simulation/{id}")
public Simulation getSimulation(@PathVariable("id") Long id) {
        Simulation simulation = null;
        try {
            Optional simulationOptional =
                    Optional.ofNullable(simulationDao.findById(BigInteger.valueOf(id)));
            if (simulationOptional.isPresent()) {
                simulation = simulationOptional.get();
            }
        } catch (Exception ex) {
   (...)

No comments: