Cómo devolver el valor en OrElse en opcional?

Krazim00da:

Tengo objeto opcional

Optional<Instrument> instrument = instrumentService.findOne(id);

Y tengo dos escenarios. En primer lugar volver ResponseEntity.ok si el objeto está presente y la segunda es ResponseEntity.noContent retorno (). Build (). Tengo función

instrument.map(instrument1 -> {
            return ResponseEntity.ok(instrumentMapper.toDto(instrumentRepository.save(instrument1)));
        })
        .//return noContent here;

Lo que tengo que escribir después de punto para devolver parte necesaria?

Andrónico:

Se puede utilizar orElse:

return instrument
    .map(instrument1 -> instrument1 -> { 
        instrument1.setValue(value); 
        return ResponseEntity.ok(instrumentMapper.toDto(instrumentRepository.save(instrument1))); })
    .orElse(ResponseEntity.noContent().build());

Y de impulsar el desarrollo del valor por defecto:

return instrument
    .map(instrument1 -> instrument1 -> { 
        instrument1.setValue(value); 
        return ResponseEntity.ok(instrumentMapper.toDto(instrumentRepository.save(instrument1))); })
    .orElseGet(() -> {
        Intrument inst = new Intrument(); 
        inst.setTitle("Not found"); 
        return inst; 
    });

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=373362&siteId=1
Recomendado
Clasificación