1 回答

TA貢獻1893條經驗 獲得超10個贊
我的下面的方法是假設findAsyncById方法 returns CompletableFuture<Optional<ChildDTO>>。所以你可以使用過濾器檢查lastName是否為空,如果為空則拋出異常orElseThrow
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
// If child last name is empty then return empty optional
.filter(child->!child.getLastName())
// If optional is empty then throw exception
.orElseThrow(CurrentDataNotFoundException::new))
添加回答
舉報